Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1. //========================Main=============================//
  2. import java.lang.reflect.Method;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Random;
  6.  
  7.  
  8. public class Main {
  9.  
  10.     /**
  11.      * @param args
  12.      */
  13.     public static void main(String[] args) throws Exception{
  14.         // TODO Auto-generated method stub
  15.         List<Integer> lista = new ArrayList<Integer>();
  16.         Random generator = new Random();
  17.    
  18.         for(int i =0 ; i < 10 ; i++){
  19.             lista.add(generator.nextInt(10));
  20.            
  21.         }
  22.        
  23.         System.out.println(lista);
  24.         System.out.println(lista.size());
  25.         System.out.println(lista.get(lista.size()-1));
  26.        
  27.         System.out.println(MaxSearchAlgorithms.scanLeftToRight(lista));
  28.         System.out.println(MaxSearchAlgorithms.scanRightToLeft(lista));
  29.         System.out.println(MaxSearchAlgorithms.scanfirstEvenThenOddIndexesScan(lista));
  30.        
  31.         //=====================================================
  32.         Class reflectClass = MaxSearchAlgorithms.class;
  33.        
  34.         /**
  35.          * jakie mamy metody
  36.          */
  37.        
  38.         Method[] methods = reflectClass.getMethods();
  39.        
  40.         for(Method met: methods){
  41.             System.out.println("metoda: "+met);
  42.         }
  43.         //=======================================================
  44.        
  45.         Object Instance = reflectClass.newInstance();
  46.         Method myMethod1 = reflectClass.getDeclaredMethod("scanRightToLeft", new Class[]{int[].class});
  47.         ArrayList max1 = new ArrayList<>();
  48.         max1 = (ArrayList) myMethod1.invoke(Instance, lista);
  49.         System.out.println(max1);
  50.  
  51.         Method myMethod2 = reflectClass.getDeclaredMethod("scanfirstEvenThenOddIndexesScan", new Class[]{int[].class});
  52.         ArrayList max2 = new ArrayList<>();
  53.         max2 = (ArrayList)myMethod2.invoke(Instance, lista);
  54.         System.out.println(max2);
  55.  
  56.         Method myMethod3 = reflectClass.getDeclaredMethod("scanLeftToRight",new Class[]{int[].class});
  57.         ArrayList max3 = new ArrayList<>();
  58.         max3 = (ArrayList) myMethod3.invoke(Instance, lista);
  59.         System.out.println(max3);
  60.        
  61.         //=======================================================
  62.        
  63.    
  64.        
  65.  
  66.     }
  67.  
  68. }
  69.  
  70.  
  71. //================================MaxSearchAlgorithms==========================
  72.  
  73. import java.util.ArrayList;
  74. import java.util.List;
  75.  
  76.  
  77. public class MaxSearchAlgorithms {
  78.     public static List scanLeftToRight(List lista){
  79.        
  80.         List<Integer> max = new ArrayList<Integer>();
  81.         max.add((int) lista.get(0));
  82.         int temp;
  83.        
  84.         for(int i = 0; i<lista.size(); i++){
  85.             temp=(int) lista.get(i);
  86.             if((int)max.get(max.size()-1) < temp){
  87.                 max.add(temp);
  88.             }
  89.         }
  90.        
  91.         return max;
  92.     }
  93.    
  94.     public static List scanRightToLeft(List lista){
  95.        
  96.         List<Integer> max = new ArrayList<Integer>();
  97.         max.add((int) lista.get(lista.size()-1));
  98.         int temp;
  99.        
  100.         for(int i = lista.size()-1; i >= 0 ; i--){
  101.             //System.out.println("index: " + i);
  102.             temp = (int)lista.get(i);
  103.             if((int)max.get(max.size()-1) < temp){
  104.                 max.add(temp);
  105.             }
  106.         }
  107.        
  108.         return max;
  109.     }
  110.    
  111.     public static List<Integer> scanfirstEvenThenOddIndexesScan(List lista){
  112.         List<Integer> maximums = new ArrayList<Integer>();
  113.         if( lista != null ){
  114.             maximums.add( (int)lista.get(0) );
  115.             for( int i = 2; i < lista.size(); i += 2 ){
  116.                 if( (int)lista.get(i) > maximums.get( maximums.size() - 1 ) ){
  117.                     maximums.add( (int)lista.get(i) );
  118.                 }
  119.             }
  120.             for( int i = 1; i < lista.size(); i += 2 ){
  121.                 if( (int)lista.get(i) > maximums.get( maximums.size() - 1 ) ){
  122.                     maximums.add( (int)lista.get(i) );
  123.                 }
  124.             }
  125.             return maximums;
  126.         }
  127.         return null;
  128.     }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement