Lara

Arrays

Tuesday(10-01-2017)

package com.lara;

 

public class Manager1

{

          public static void main(String[] args)

          {

                   int[] x = new int[10];

                   System.out.println(x[0]);

                   System.out.println(x[1]);

                   System.out.println(x[3]);

                   System.out.println(x[6]);

                   System.out.println(x[9]);

          }

}

package com.lara;

 

public class Manager2

{

          public static void main(String[] args)

          {

                   int[] x;

                   x = new int[3];

                   System.out.println(x[0]);

                   System.out.println(x[1]);

                   System.out.println(x[2]);

                   x[0] = 10;

                   x[1] = 20;

                   x[2] = 30;

                   System.out.println(x[0]);

                   System.out.println(x[1]);

                   System.out.println(x[2]);

 

          }

}

package com.lara;

 

public class Manager3

{

          public static void main(String[] args)

          {

                   double[] x = new double[4];

                   for(int i = 0; i < x.length; i++)

                   {

                             System.out.println(x[i]);

                   }

                   x[0] = 1.2;

                   x[1] = 0.2;

                   x[2] = 3.4;

                   x[3] = 5.3;

                   for(int i = 0; i < x.length; i++)

                   {

                             System.out.println(x[i]);

                   }

 

          }

}

package com.lara;

 

public class Manager4

{

          public static void main(String[] args)

          {

                   String[] x = new String[10];

                   for(int i = 3; i <= 6; i++)

                   {

                             System.out.println(x[i]);

                   }

                   x[2] = "abc";

                   x[6] = "xyz";

                   for(int i = 1; i < 8; i++)

                   {

                             System.out.println(x[i]);

                   }

                  

          }

}

package com.lara;

 

public class Manager5

{

          public static void main(String[] args)

          {

                   String[] x = new String[6];

                  

                   for(String s1 : x)

                   {

                             System.out.println(s1);

                   }

                   System.out.println("-----------");

                   x[2] = "abc";

                   x[4] = "xyz";

                   for(String s1 : x)

                   {

                             System.out.println(s1);

                   }

                  

                  

          }

}

package com.lara;

 

public class Manager6

{

          public static void main(String[] args)

          {

                   double[] x = {10.2, 20.9, 30.5, 1.2, 4, 5, 10};

                   for(double i : x )

                   {

                             System.out.println(i);

                   }

          }

}

package com.lara;

 

public class Manager7

{

          public static void main(String[] args)

          {

                   int[] x = new int[2];

                   int[] y = x;

                   x[0] = 100;

                   y[1] = 200;

                   System.out.println(y[0]);

                   System.out.println(x[1]);

          }

}

package com.lara;

 

public class Manager8

{

          static void test(int[] x)

          {

                   x[2] = 20;

          }

          public static void main(String[] args)

          {

                   int[] y = new int[3];

                   System.out.println(y[2]);

                   test(y);

                   System.out.println(y[2]);

          }

}

package com.lara;

 

public class Manager9

{

          public static void main(String[] args)

          {

                   int[] x = new int[]{10, 20, 30, 110};

                   for(int i = 0; i < x.length; i++)

                   {

                             System.out.println(x[i]);

                   }

          }

}

package com.lara;

 

public class Manager10

{

          public static void main(String[] args)

          {

                   test(new int[]{10, 20, 30});

          }

          static void test(int[] x)

          {

                   for(int i : x)

                   {

                             System.out.println(i);

                   }

          }

}

package com.lara;

 

import java.util.Arrays;

 

public class Manager11

{

          public static void main(String[] args)

          {

                   int[] x = new int[2];

                   System.out.println(x);

                   System.out.println(Arrays.toString(x));

          }

}

package com.lara;

 

import java.util.Arrays;

 

public class Manager12

{

          public static void main(String[] args)

          {

                   double[] x = {10.7, 12.7, 2.3, 9.8};

                   System.out.println(x);

                   System.out.println(Arrays.toString(x));

          }

}

package com.lara;

 

public class Manager13

{

          public static void main(String[] args)

          {

                   int[] x = new int[2];

                   Object y = x;

                   //System.out.println(y[0]);

                   int[] z = (int[]) y;

                   System.out.println(z[0]);

          }

}

package com.lara;

 

import java.util.Arrays;

 

public class Manager14

{

          public static void main(String[] args)

          {

                   int[] x = {10, 200, 30, 4, 25, 100};

                   System.out.println(Arrays.toString(x));

                   Arrays.sort(x);

                   System.out.println(Arrays.toString(x));

          }

}

package com.lara;

 

import java.util.Arrays;

import java.util.Collections;

 

public class Manager15

{

          public static void main(String[] args)

          {

                   Integer[] x = {12, 67, 90, 1, 45, 76, 5};

                   System.out.println(Arrays.toString(x));

                   Arrays.sort(x);

                   System.out.println(Arrays.toString(x));

                   Arrays.sort(x, Collections.reverseOrder());

                   System.out.println(Arrays.toString(x));

          }

}

package com.lara;

 

import java.util.Arrays;

import java.util.Collections;

 

public class Manager16

{

          public static void main(String[] args)

          {

                   String[] x = {"pqr", "java", "lara", "xyz", "abc", "test", "blr"};

                   System.out.println(Arrays.toString(x));

                   Arrays.sort(x);

                   System.out.println(Arrays.toString(x));

                   Arrays.sort(x, Collections.reverseOrder());

                   System.out.println(Arrays.toString(x));

                  

          }

}

 

Wednesday(11-01-2017)

package com.lara;

 

import java.util.Arrays;

 

class A

{

          int i;

          A(int i)

          {

                   this.i = i;

          }

}

public class Manager17

{

          public static void main(String[] args)

          {

                   A[] elements = {new A(90), new A(9), new A(10)};

                   System.out.println(Arrays.toString(elements));

          }

}

package com.lara;

 

import java.util.Arrays;

 

class B

{

          int i;

          B(int i)

          {

                   this.i = i;

          }

          public String toString()

          {

                   return "i = " + i;

          }

}

public class Manager18

{

          public static void main(String[] args)

          {

                   B[] elements = {new B(9), new B(10), new B(5), new B(20)};

                   System.out.println(Arrays.toString(elements));

          }

}

package com.lara;

 

import java.util.Arrays;

 

class C

{

          int i, j;

          C(int i, int j)

          {

                   this.i = i;

                   this.j = j;

          }

          public String toString()

          {

                   return "(i=" + i + ", j=" + j + ")";

          }

}

public class Manager19

{

          public static void main(String[] args)

          {

                   C[] elements = {new C(12, 2), new C(0, 5), new C(90, 24)};

                   System.out.println(Arrays.toString(elements));

          }

}

package com.lara;

 

import java.util.Arrays;

 

public class Manager20

{

          public static void main(String[] args)

          {

                   String[] arr1 = {"pqr", "abc", "xyz"};

                   Integer[] arr2 = {10, 2, 8};

                   Double[] arr3 = {30.9, 20.8, 15.8};

                  

                   System.out.println(Arrays.toString(arr1));

                   Arrays.sort(arr1);

                   System.out.println(Arrays.toString(arr1));

                   System.out.println("---------");

                  

                   System.out.println(Arrays.toString(arr2));

                   Arrays.sort(arr2);

                   System.out.println(Arrays.toString(arr2));

                   System.out.println("---------");

                  

                   System.out.println(Arrays.toString(arr3));

                   Arrays.sort(arr3);

                   System.out.println(Arrays.toString(arr3));

                   System.out.println("---------");

                            

          }

}

package com.lara;

 

import java.util.Arrays;

 

class D

{

          int i;

          D(int i)

          {

                   this.i = i;

          }

          public String toString()

          {

                   return "i = " + i;

          }

}

public class Manager21

{

          public static void main(String[] args)

          {

                   D[] elements = {new D(90), new D(10), new D(50)};

                   System.out.println(Arrays.toString(elements));

                   Arrays.sort(elements);

                   System.out.println(Arrays.toString(elements));

          }

}

package com.lara;

 

import java.util.Arrays;

 

class E implements Comparable

{

          int i;

          E(int i)

          {

                   this.i = i;

          }

          public String toString()

          {

                   return "i = " + i;

          }

          @Override

          public int compareTo(Object o)

          {

                   return i - ((E)o).i;

          }

}

public class Manager22

{

          public static void main(String[] args)

          {

                   E[] elements = {new E(10), new E(20), new E(5)};

                   System.out.println(Arrays.toString(elements));

                   Arrays.sort(elements);

                   System.out.println(Arrays.toString(elements));

          }

}

package com.lara;

 

import java.util.Arrays;

 

class F implements Comparable

{

          int i, j;

          F(int i, int j)

          {

                   this.i = i;

                   this.j = j;

          }

          public String toString()

          {

                   return "(" + i + "," + j + ")";

          }

         

          @Override

          public int compareTo(Object o)

          {

                   return j - ((F)o).j;

          }       

}

public class Manager23

{

          public static void main(String[] args)

          {

                   F[] elements =           {

                                                                   new F(10, 2),

                                                                   new F(0, 20),

                                                                   new F(20, 12),

                                                                   new F(2, 0),

                                                          };

                  

                   System.out.println(Arrays.toString(elements));

                   Arrays.sort(elements);

                   System.out.println(Arrays.toString(elements));

          }

}

package com.lara;

 

import java.util.Arrays;

 

class G implements Comparable<G>

{

          int i, j;

          G(int i, int j)

          {

                   this.i = i;

                   this.j = j;

          }

          public String toString()

          {

                   return "(" + i + "," + j + ")";

          }

          @Override

          public int compareTo(G o)

          {

                   return o.i - i;

          }

}

public class Manager24

{

          public static void main(String[] args)

          {

                   G[] elements = {new G(1, 0), new G(10, 20), new G(5, 100)};

                   System.out.println(Arrays.toString(elements));

                   Arrays.sort(elements);

                   System.out.println(Arrays.toString(elements));

          }

}

package com.lara;

 

import java.util.Arrays;

import java.util.Comparator;

 

class H

{

          int x, y;

          H(int x, int y)

          {

                   this.x = x;

                   this.y = y;

          }

          public String toString()

          {

                   return "(" + x + "," + y + ")";

          }       

}

class I implements Comparator

{

          @Override

          public int compare(Object o1, Object o2)

          {

                   return ((H)o1).x - ((H)o2).x;

          }

}

class J implements Comparator

{

          @Override

          public int compare(Object o1, Object o2)

          {

                   return ((H)o1).y - ((H)o2).y;

          }

}

public class Manager25

{

          public static void main(String[] args)

          {

                   H[] elements = {

                                                                   new H(1, 2),

                                                                   new H(2, 1),

                                                                   new H(10, 3),

                                                                   new H(0, 20),

                                                                   new H(5, 0)

                                                   };

                   System.out.println(Arrays.toString(elements));

                   Arrays.sort(elements, new I());

                   System.out.println(Arrays.toString(elements));

                   Arrays.sort(elements, new J());

                   System.out.println(Arrays.toString(elements));

          }

}

package com.lara;

 

import java.util.Arrays;

import java.util.Comparator;

 

class Person

{

          int age;

          int salary;

          Person(int age, int salary)

          {

                   this.age = age;

                   this.salary = salary;

          }

          public String toString()

          {

                   return "(" + age + "," + salary + ")";

          }

}

class SortBasedOnAge implements Comparator

{

          @Override

          public int compare(Object o1, Object o2)

          {

                   return ((Person)o1).age - ((Person)o2).age;

          }

}

class SortBasedOnSalary implements Comparator

{

          @Override

          public int compare(Object o1, Object o2)

          {

                   return ((Person)o1).salary - ((Person)o2).salary;

          }

}

 

public class Manager26

{

          public static void main(String[] args)

          {

                   Person[] persons = {

                                                                             new Person(22, 50000),

                                                                             new Person(25, 45000),

                                                                             new Person(23, 60000),

                                                                             new Person(20, 55000)

                                                             };

                   System.out.println(Arrays.toString(persons));

                   Arrays.sort(persons, new SortBasedOnAge());

                   System.out.println(Arrays.toString(persons));

                   Arrays.sort(persons, new SortBasedOnSalary());

                   System.out.println(Arrays.toString(persons));

 

          }

}