Java Array and Method

Java Array and Method

We normally use a data type to store one value, but sometimes we need to store many values and use them later. In such cases, we can use methods and arrays.

Array

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

		Example: int[] num = {1,2,3,4};        

The above example is a one-dimensional array type, and another type is a multi-dimensional array.

		Example: int [][] ={
					{1,3,6},
					{3,4,5}
				   }        

One-dimensional Array and Multi-dimensional Array

We can use a "for loop" data insert and retrieve data in an index array and a multi-dimensional array.

one dimensional Array

This type of class has an index array. Its index count equals array length-1.

int [] num = new int [5] is one option to create a one-dimensional array. i did explain to above example another option to create a one-dimensional array.

I am first explaining data retrieving array using a for loop.

 int[] num= {10, 20, 30, 40, 50};

	//option 1 
          for (int number : num) {
            System.out.print(number  +",");
        }

	//option 2
          for (int i=0; i< num.length; i++) {
            System.out.print(num[i]);
        }        

Add to the data, not using a loop.

	int[] num = new int[5];

        num[0] = 67;
        num[1] = 34;
        num[2] = 45;
        num[3] = 60;
        num[4] = 50;

        System.out.print("[");
        for (int i = 0; i < num.length; i++) {
            System.out.print(num[i] + ", ");
        }
        System.out.print("\b\b]");        

Add data using a loop.

 	int x = 2468;
        int[] num = new int[5];
        int  index =0;

        while (x != 0 && index < num.length) {
            int j = x % 10;
            num[index] = j;
            x= x/10;
            index++;
        }

        System.out.print("[");
        for (int i = 0; i < index; i++) {
            System.out.print(num[i] + ", ");
        }
        System.out.print("\b\b]");        

multi-dimensional array

int [][] num= new int [5][3] is this one option to create multi-dimensional array. I did explain in the above example another option to create a multi-dimensional array.

    int x = 246456567;

        int[][] num = new int[3][3];
  
             for (int i = 0; i < num.length; i++) {
                 for (int j = 0; j < num[i].length; j++) {
                     if (x != 0) {
                         num[i][j] = x % 10;
                         x = x / 10;
                  }
              }
          }

        System.out.println("{");

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

            System.out.print("{");

            for(int j = 0; j <  num[i].length;j++){
                  System.out.print(num[i][j] + ", ");
            }
              System.out.println("\b\b},");
        }
        System.out.print("}");
    }        

Java Array Expansion – Increasing Array Size and Adding New Elements



        int[] numbers = {10, 20, 30, 40, 50};

        for (int number : numbers) {
            System.out.print(number + ",");
        }
        int x = numbers.length;

        int[] oldnumbers = new int[5];
        oldnumbers[0] = numbers[0];
        oldnumbers[1] = numbers[1];
        oldnumbers[2] = numbers[2];
        oldnumbers[3] = numbers[3];
        oldnumbers[4] = numbers[4];

        x = x + 1;
        System.out.println();
        numbers = new int[x];

        numbers[0] = oldnumbers[0];
        numbers[1] = oldnumbers[1];
        numbers[2] = oldnumbers[2];
        numbers[3] = oldnumbers[3];
        numbers[4] = oldnumbers[4];
        numbers[5] = 60;

        for (int number : numbers) {
            System.out.print(number + ",");
        }        

When you copy the reference but later create a new array, the old one still remembers its identity. Memory addresses matter! Below code sample

public class ArrayIdentityCheck {
    public static void main(String[] args) {
        Integer[] numbers = {10, 20, 30, 40, 50};

        System.out.println("Original array reference: " + System.identityHashCode(numbers));

        Integer[] oldNumbers = numbers; // Save the reference

        int x = numbers.length + 1;
        numbers = new Integer[x]; // Now numbers points to a new array

        System.out.println("New array reference: " + System.identityHashCode(numbers));
        System.out.println("Old array reference from backup: " + System.identityHashCode(oldNumbers));

        System.out.println("Are old and new arrays same? " + (numbers == oldNumbers));
    }
}        

Method

We can define methods using access modifiers like public, private, etc.

The main types of methods are void methods and return type methods.

These can be further divided into static and non-static methods.

We can call a static method using the class name, but a non-static method cannot be called using the class name — instead, we must create an object and then call it.

We can use return to give back either a single value or an array from a method. We can also create an array inside a process and let the user decide the array size using input.

method and user input, and array

 public static int getNumber() {
        return 5;
    }

    public static int[] createArray() {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter array size: ");
        int size = sc.nextInt();

        int[] arr = new int[size];

        for (int i = 0; i < size; i++) {
            System.out.print("Enter value for index " + i + ": ");
            arr[i] = sc.nextInt();
        }
        return arr;
    }

    public static void main(String[] args) {

        System.out.println("Single value: " + getNumber());
        int[] numbers = createArray();
        System.out.print("Array values: ");

        for (int n : numbers) {
            System.out.print(n + " ");
       }
    }

        

To view or add a comment, sign in

Others also viewed

Explore content categories