Advertisement
Mancolo

ArrayList

Jan 4th, 2022
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. public static  <T> void count(T[] a) {
  2.         int odd = 0;
  3.         int even = 0;
  4.         for (int i = 0; i < a.length; i++) {
  5.             if (a[i] instanceof Integer) {
  6.                 if ((int) a[i] % 2 == 1) {
  7.                     odd += 1;
  8.                 }
  9.                 if ((int) a[i] % 2 == 0) {
  10.                     even += 1;
  11.                 }
  12.             }
  13.         }
  14.         System.out.println("Even: " + even);
  15.         System.out.println("Odd: " + odd);
  16.     }
  17.  
  18.  
  19.  
  20.     public static <T> void swap(ArrayList<T> a){
  21.         Scanner in = new Scanner(System.in);
  22.         System.out.print("Enter number of first element: ");
  23.         int m1 = in.nextInt();
  24.         System.out.print("Enter number of second element: ");
  25.         int m2 = in.nextInt();
  26.         Collections.swap(a, m1-1, m2-1);
  27.         for (T c:
  28.              a) {
  29.             System.out.println(c);
  30.         }
  31.     }
  32.  
  33.  
  34.  
  35.     public static <T> void maxmini(ArrayList<T> a){
  36.         Scanner in = new Scanner(System.in);
  37. System.out.print("Enter begining: ");
  38.         int m1 = (in.nextInt())-1;
  39.         System.out.print("Enter ending: ");
  40.         int m2 = (in.nextInt())-1;
  41.         ArrayList<Integer>asd = new ArrayList<>();
  42.         for (int i = m1; i < m2;i++){
  43.             if (a.get(i) instanceof Integer){
  44.             asd.add((Integer) a.get(i));
  45.  
  46.         }}
  47.         System.out.println(Collections.min(asd));
  48.         System.out.println(Collections.max(asd));
  49.  
  50.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement