QA's approach 2 Java - Arrays & ArrayLists_TBD


Arrays:

Useful methods Arrays class

java.util.Arrays.asList(a);

java.util.Arrays.binarySearch(a, key);

java.util.Arrays.fill(a, val);

java.util.Arrays.fill(a, fromIndex, toIndex, val);

java.util.Arrays.sort(a);

java.util.Arrays.toString(a);

java.util.Arrays.copyOf(original, newLength);

java.util.Arrays.equals(a, a2);


Basic Example to learn how to Declare, Initialize & Iterate over the elements in an array

package dataStructureExmpls;

public class ArrayBasicExample {
public static void main(String[] args) {

// Declare an array
int[] number;

// provide memory for an array
number = new int[5];

//Array of reference type
Student[] s=new Student[5];

int[ ][ ] arr = new int[3][ ];

arr[0] = new int[3];

arr[1] = new int[5];

arr[2] = new int[4];
// memory allocation for an array
// int[] num=new int[10];
// Initializing the array at the time of declaration
int age[] = { 10, 20, 30, 40 };

int[][] n = new int[3][4];
int[] n2 = new int[n.length];
// 2Dimensional array
int[][] s = { { 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10 } };

// For Each loop applied on 2 dimensional array

for (int[] num : s) {
System.out.println(Arrays.toString(num));
for (int data : num) {
System.out.println(data);
}

}

}

}

Copying array to another array

A shallow copy just copies the values of the references in the class, hence modifying the content in first array from which values are copied will impact the content in the copied array.

A deep copy copies the values. A deep copy copies the values creating the new array object.
Shallow Copy:

class CopyArray {

public static void main(String[] args) {

int [] numbers = {1, 2, 3, 4, 5, 6};

int [] positiveNumbers = numbers; // copying arrays
for (int number: positiveNumbers) {

System.out.print(number + ", ");
}

numbers[0] = -1;
for (int number: positiveNumbers) {

System.out.print(number + ", ");

}
}
}

When the first element of numbers array is changed to -1, the first element of positiveNumbers array also becomes -1. It's because both arrays refers to the same array object.
Deep Copy

Using loops to copy Arrays

import java.util.Arrays;
class ArraysCopy {

public static void main(String[] args) {
int [] source = {1, 2, 3, 4, 5, 6};

int [] destination = new int[6];
for (int i = 0; i < source.length; ++i) {

destination[i] = source[i];

}
// converting array to string
System.out.println(Arrays.toString(destination));

}
}
The source and destination array doesn't share the same reference (deep copy). Meaning, if elements of one array (either source or destination) is changed, corresponding elements of another array is unchanged.

The toString() method is used to convert array to string (for the purpose of output only).

ArrayCopy:

The System class contains arraycopy() method that allows you to copy data from one array to another.
The arraycopy() method is efficient as well as flexible. The method allows you to copy a specified portion of the source array to the destination array.

public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

Here,
src - array you want to copy
srcPos - starting position (index) in the src array
dest - elements of src array will be copied to this array
destPos - starting position (index) in the dest array
length - number of elements to copy

import java.util.Arrays;
class ArraysCopy {

public static void main(String[] args) {

int[] n1 = { 2, 3, 12, 4, 12, -2 };

int[] n3 = new int[5];
// Creating n2 array of having length of n1 array

int[] n2 = new int[n1.length];

// copying entire n1 array to n2

System.arraycopy(n1, 0, n2, 0, n1.length);
System.out.println("n2 = " + Arrays.toString(n2));
// copying elements from index 2 on n1 array

// copying element to index 1 of n3 array

// 2 elements will be copied
System.arraycopy(n1, 2, n3, 1, 2);

System.out.println("n3 = " + Arrays.toString(n3));

}

}

copyOfRange()

copyOfRange() method defined in java.util.Arrays class to copy arrays. You do not need to create the destination array before this method is called.

// To use toString() and copyOfRange() method
import java.util.Arrays;

class ArraysCopy {

public static void main(String[] args) {

int[] source = { 2, 3, 12, 4, 12, -2 };
// copying entire source array to destination

int[] destination1 = Arrays.copyOfRange(source, 0, source.length);

System.out.println("destination1 = " + Arrays.toString(destination1));

// copying from index 2 to 5 (5 is not included)

int[] destination2 = Arrays.copyOfRange(source, 2, 5);

System.out.println("destination2 = " + Arrays.toString(destination2));
}

}

Click Here to know more on more on the Methods available in Class/Interface in Collection API

Comments

Popular posts from this blog

QA's approach 2 Java - Understanding Static context

Selenium 4 absolute beginners - How to create Batch execution file

Technologies - Log4J - Create Log4j Configuration File - Where ? How ? What ?