Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //========================Main=============================//
- import java.lang.reflect.Method;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- public class Main {
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception{
- // TODO Auto-generated method stub
- List<Integer> lista = new ArrayList<Integer>();
- Random generator = new Random();
- for(int i =0 ; i < 10 ; i++){
- lista.add(generator.nextInt(10));
- }
- System.out.println(lista);
- System.out.println(lista.size());
- System.out.println(lista.get(lista.size()-1));
- System.out.println(MaxSearchAlgorithms.scanLeftToRight(lista));
- System.out.println(MaxSearchAlgorithms.scanRightToLeft(lista));
- System.out.println(MaxSearchAlgorithms.scanfirstEvenThenOddIndexesScan(lista));
- //=====================================================
- Class reflectClass = MaxSearchAlgorithms.class;
- /**
- * jakie mamy metody
- */
- Method[] methods = reflectClass.getMethods();
- for(Method met: methods){
- System.out.println("metoda: "+met);
- }
- //=======================================================
- Object Instance = reflectClass.newInstance();
- Method myMethod1 = reflectClass.getDeclaredMethod("scanRightToLeft", new Class[]{int[].class});
- ArrayList max1 = new ArrayList<>();
- max1 = (ArrayList) myMethod1.invoke(Instance, lista);
- System.out.println(max1);
- Method myMethod2 = reflectClass.getDeclaredMethod("scanfirstEvenThenOddIndexesScan", new Class[]{int[].class});
- ArrayList max2 = new ArrayList<>();
- max2 = (ArrayList)myMethod2.invoke(Instance, lista);
- System.out.println(max2);
- Method myMethod3 = reflectClass.getDeclaredMethod("scanLeftToRight",new Class[]{int[].class});
- ArrayList max3 = new ArrayList<>();
- max3 = (ArrayList) myMethod3.invoke(Instance, lista);
- System.out.println(max3);
- //=======================================================
- }
- }
- //================================MaxSearchAlgorithms==========================
- import java.util.ArrayList;
- import java.util.List;
- public class MaxSearchAlgorithms {
- public static List scanLeftToRight(List lista){
- List<Integer> max = new ArrayList<Integer>();
- max.add((int) lista.get(0));
- int temp;
- for(int i = 0; i<lista.size(); i++){
- temp=(int) lista.get(i);
- if((int)max.get(max.size()-1) < temp){
- max.add(temp);
- }
- }
- return max;
- }
- public static List scanRightToLeft(List lista){
- List<Integer> max = new ArrayList<Integer>();
- max.add((int) lista.get(lista.size()-1));
- int temp;
- for(int i = lista.size()-1; i >= 0 ; i--){
- //System.out.println("index: " + i);
- temp = (int)lista.get(i);
- if((int)max.get(max.size()-1) < temp){
- max.add(temp);
- }
- }
- return max;
- }
- public static List<Integer> scanfirstEvenThenOddIndexesScan(List lista){
- List<Integer> maximums = new ArrayList<Integer>();
- if( lista != null ){
- maximums.add( (int)lista.get(0) );
- for( int i = 2; i < lista.size(); i += 2 ){
- if( (int)lista.get(i) > maximums.get( maximums.size() - 1 ) ){
- maximums.add( (int)lista.get(i) );
- }
- }
- for( int i = 1; i < lista.size(); i += 2 ){
- if( (int)lista.get(i) > maximums.get( maximums.size() - 1 ) ){
- maximums.add( (int)lista.get(i) );
- }
- }
- return maximums;
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement