1. Declare Array method 1:
String[] str = {"David", "a", "TX", "123", "Google"};
int[] num = {1,5,12,100,1000};
System.out.println(Arrays.toString(str));
System.out.println(Arrays.toString(num));
System.out.println(Arrays.asList(num));
Output:
[David, a, TX, 123, Google]
[1, 5, 12, 100, 1000]
[[I@2d363fb3]
2. Declare Array method 2:
String[] str = new String[5];
str[0]="David";
str[1]="a";
str[2]="TX";
str[3]="123";
str[4]="Google";
System.out.println("String Array = " +Arrays.toString(str));
int[] num = new int[5];
num[0]=1;
num[1]=51;
num[2]=111;
num[3]=0;
num[4]=1234;
System.out.println("Int Array = " +Arrays.toString(num));
Output:
String Array = [David, a, TX, 123, Google]
Int Array = [1, 51, 111, 0, 1234]
3. Sorting Array
String[] str = {"David", "a", "TX", "123", "Google"};
int[] num = {1,8,0,999,111};
Arrays.sort(str);
Arrays.sort(num);
System.out.println("String Array = " +Arrays.toString(str2));
System.out.println("Int Array = " +Arrays.toString(num));
Output;
String Array = [David, a, TX, 123, Google]
Int Array = [0, 1, 8, 111, 999]
4. Comparing arrays
String[] str1 = {"David", "a", "TX", "123", "Google"};
String[] str2 = {"David", "a", "TX", "123", "Google"};
int[] num1 = {1,8,0,999,111};
int[] num2 = {1,8,0,999,111};
if(Arrays.equals(str1, str2)) {
System.out.println("The two string arrays are equal.");
}
if(Arrays.equals(num1, num2)) {
System.out.println("The two num arrays are equal.");
}
Output:
The two string arrays are equal.
The two num arrays are equal.
String[] str1 = {"David", "a", "TX", "123", "Google"};
String[] str2 = {"David", "a", "123", "TX", "Google"};
int[] num1 = {1,8,0,999,111};
int[] num2 = {1,8,999,111};
if(Arrays.equals(str1, str2)) {
System.out.println("The two string arrays are equal.");
} else {
System.out.println("The two string arrays are NOT equal.");
}
if(Arrays.equals(num1, num2)) {
System.out.println("The two num arrays are equal.");
} else {
System.out.println("The two num arrays are NOT equal.");
}
Output:
The two string arrays are NOT equal.
The two num arrays are NOT equal.
5. Copying Arrays
String[] str1 = {"David", "a", "123", "TX", "Google"};
String[] str2 = new String[5];
//Copying
str2 = str1;
System.out.println("The copied array = "+Arrays.toString(str2));
Output:
The copied array = [David, a, 123, TX, Google]
6. Copying arrays replaces the values in second array
String[] str1 = {"David", "a", "123", "TX", "Google"};
String[] str2 = {"John", "TX"};
//copying replaces the values
str2 = str1;
System.out.println("The copied array = "+Arrays.toString(str2));
Output:
The copied array = [David, a, 123, TX, Google]
--------------------------Old Contents----------------------------
package Interview ;
import java.util.Arrays;
public class CompareArray {
public static void main(String[] args)
{
// String[] elements = new String[]{"element1", "element2", "element3"};
//OR//
String[] stringElements = {"element1", "element2", "element3", "element4"};
int[] intElements = {11,12,13,14,15};
//all elements in the array
System.out.println("All String elements = "+ Arrays.asList(stringElements));
//System.out.println("All int elements = "+ Arrays.asList(intElements));
System.out.println("index of element3 = "+ Arrays.asList(stringElements).indexOf("element3"));
System.out.println("index of 14 = "+ Arrays.asList(intElements).indexOf(14));
System.out.println("String value at index 2 = "+ Arrays.asList(stringElements[2]));
System.out.println("int value at index 2 = "+ Arrays.asList(intElements[2]));
String[] chrElements = {"element1", "element2", "element3", "element4"};
// compare arrays, right approach
if (Arrays.equals(stringElements, chrElements)) {
System.out.println("Third Test, Right Approach, Same Arrays");
return ; // takes you out of the method if satisfied
} else {
System.out.println("Third Test, NOT Same Arrays");
}
//compare arrays
if (stringElements==chrElements){
System.out.println("First Test, Same Arrays");
return ;
} else {
System.out.println("First Test, NOT Same Arrays");
}
//compare arrays
if (stringElements.equals(chrElements)){
System.out.println("Second Test, Same Arrays");
return ; // takes you out of the method if satisfied
} else {
System.out.println("Second Test, NOT Same Arrays");
}
//check if an element is present in array
System.out.println("array length = "+stringElements.length);
for(int a=0; a<stringElements.length; a++){
if("element3" == stringElements[a] ){
System.out.println("Element Present");
return ;//or break; // takes you out of the method if satisfied
} else {
System.out.println("Element NOT Present");
}
}
}
}
Output1:
All String elements = [element1, element2, element3, element4]
index of element3 = 2
index of 14 = -1
String value at index 2 = [element3]
int value at index 2 = [13]
Third Test, Right Approach, Same Arrays
When you comment out or remove the “return” in first compare code
Output2:
All String elements = [element1, element2, element3, element4]
index of element3 = 2
index of 14 = -1
String value at index 2 = [element3]
int value at index 2 = [13]
Third Test, Right Approach, Same Arrays
First Test, NOT Same Arrays
Second Test, NOT Same Arrays
array length = 4
Element NOT Present
Element NOT Present
Element Present
No comments:
Post a Comment