Pages

Java Arrays

Array Description : Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. Array can contain primitives as well as object or non-primitive references of a class depending on the definition of the array.

1. Array Integer Primitive Example

public class MethodOne {
      public static void main(String [ ] args) {
            int [ ] in = {1, 2, 3, 4, 5, 6};
            System.out.println("Display Arrays Values : ");
            System.out.println("First Elements : " + in[0]);
            System.out.println("Second Elements : " + in[1]);
            System.out.println("Third Elements : " + in[2]);
            System.out.println("Fourth Elements : " + in[3]);
            System.out.println("Fifth Elements : " + in[4]);
            System.out.println("Sixth Elements : " + in[5]);
      }
}

2. Array Character Primitive Example

public class MethodTwo {
      public static void main(String [ ] args) {
            char [ ] ch = {'A','B','C','D','E','F'};
            System.out.println("Display Arrays Values : ");
            System.out.println("First Elements : " + ch[0]);
            System.out.println("Second Elements : " + ch[1]);
            System.out.println("Third Elements : " + ch[2]);
            System.out.println("Fourth Elements : " + ch[3]);
            System.out.println("Fifth Elements : " + ch[4]);
            System.out.println("Sixth Elements : " + ch[5]);
      }
}

3. Array Byte Primitive Example

public class MethodThree {
      public static void main(String [ ] args) {
            byte [ ] bytes = {1, 2, 3, 4, 5, 6};
            System.out.println("Display Arrays Values : ");
            System.out.println("First Elements : " + bytes[0]);
            System.out.println("Second Elements : " + bytes[1]);
            System.out.println("Third Elements : " + bytes[2]);
            System.out.println("Fourth Elements : " + bytes[3]);
            System.out.println("Fifth Elements : " + bytes[4]);
            System.out.println("Sixth Elements : " + bytes[5]);
      }
}

4. Array Double Primitive Example

public class MethodFour {
      public static void main(String [ ] args) {
            double[] dou = {1.1, 1.2, 1.3, 1.4, 1.5, 1.6};
            System.out.println("Display Arrays Values : ");
            System.out.println("First Elements : " + dou[0]);
            System.out.println("Second Elements : " + dou[1]);
            System.out.println("Third Elements : " + dou[2]);
            System.out.println("Fourth Elements : " + dou[3]);
            System.out.println("Fifth Elements : " + dou[4]);
            System.out.println("Sixth Elements : " + dou[5]);
      }
}

5. Array String Object Example

public class MethodFour {
      public static void main(String [ ] args) {
            String [] str = {"One", "Two", "Three", "Four", "Five", "Six"};
            System.out.println("Display Arrays Values : ");
            System.out.println("First Elements : " + str[0]);
            System.out.println("Second Elements : " + str[1]);
            System.out.println("Third Elements : " + str[2]);
            System.out.println("Fourth Elements : " + str[3]);
            System.out.println("Fifth Elements : " + str[4]);
            System.out.println("Sixth Elements : " + str[5]);
      }
}

6. Integer Primitive Data Type Using For Loop

public class ForLoopForInteger {
      public static void main(String[] args) {
            int [] in = {1, 2, 3, 4, 5, 6};
            for(int i = 0; i < in.length; i++) {
                        System.out.println(in[i]);
            }
      }
}

7. Double Primitive Data Type Using For Loop

public class ForLoopForDouble {
      public static void main(String[] args) {
            double [] dou = {1.1, 1.2, 1.3, 1.4, 1.5, 1.6};
            for(int i = 0; i < dou.length; i++) {
                        System.out.println(in[i]);
            }
      }
}

8. Integer Primitive Data Type Using For Each Loop

public class ForEachLoopForInteger {
      public static void main(String[] args) {
            int [] in = {1, 2, 3, 4, 5, 6};
            for(int a : in) {
                        System.out.println(in[a]);
            }
      }
}

9. Byte Primitive Data Type Using For Each Loop

public class ForEachLoopForByte {
      public static void main(String[] args) {
            byte[] bytes = {1, 2, 3, 4, 5, 6};
            for (byte b : bytes) {
                  String st = String.format("%02X", b);
                  System.out.print(st);
            }
      }
}

10. Double Primitive Data Type Using For Each Loop

public class ForEachLoopForDouble {
      public static void main(String[] args) {
            double[] dou = {1.1, 1.2, 1.3, 1.4};
            for (double element: dou) {
                  System.out.println(element);
            }
      }
}

11. Compute Sum And Average for Array Program

publc class ComputeSumAverage {
      public static void main(String [ ] args) {
            int [] numbers = {2, -9, 0, 5, 12, -25, 22, 9, 8, 12};
            int sum = 0;
            Double average;
            for (int number: numbers) {
                  sum += number;
            }
            int arrayLength = numbers.length;
            average = ((double)sum / (double)arrayLength);
            System.out.println("Sum = " + sum);
            System.out.println("Average = " + average);
      }
}

12. Convert Byte Array to Hexa Decimal Value using Byte Operation

public class ConvertByteHexaDecimal {
      private final static char [ ] hexDecimal = "0123456789ABCDEF".toCharArray();
      public static String bytesToHexDecimal(byte[] bytes) {
      char[] hexChars = new char[bytes.length * 2];
      for ( int j = 0; j < bytes.length; j++ ) {
                  int v = bytes[j] & 0xFF;
                  hexChars[j * 2] = hexArray[v >>> 4];
                  hexChars[j * 2 + 1] = hexArray[v & 0x0F];
            }
            return new String(hexChars);
      }
      public static void main(String[] args) {
            byte[] bytes = {1, 2, 3, 4, 5, 6};
            String s = bytesToHexDecimal(bytes);
            System.out.println(s);
      }
}