Array Methods Flashcards
Declare an array
String[] aArray = new String[5];
String[] bArray = {“a”,”b”,”c”, “d”, “e”};
String[] cArray = new String[]{“a”,”b”,”c”,”d”,”e”};
Print an array
int[] intArray = { 1, 2, 3, 4, 5 }; String intArrayString = Arrays.toString(intArray); // print directly will print reference value System.out.println(intArray); //O/p: [I@7150bd4d System.out.println(intArrayString); // O/p: [1, 2, 3, 4, 5]
Create an ArrayList from an array
String[] stringArray = { "a", "b", "c", "d", "e" }; ArrayList arrayList = new ArrayList(Arrays.asList(stringArray)); System.out.println(arrayList); // [a, b, c, d, e]
Check if an array contains a certain value
String[] stringArray = { "a", "b", "c", "d", "e" }; boolean b = Arrays.asList(stringArray).contains("a"); System.out.println(b); // true
Find the Middle Element of the Array
int Start = 0 ;
int End = InputArray.length - 1
int Middle = Start + (End - Start)/2
Declare an array inline
method(new String[]{“a”, “b”, “c”, “d”, “e”});
Joins the elements of the provided array into a single String
// containing the provided list of elements // Apache common lang String j = StringUtils.join(new String[] { "a", "b", "c" }, ", "); System.out.println(j); // a, b, c
Convert an ArrayList to an array
String[] stringArray = { “a”, “b”, “c”, “d”, “e” };
ArrayList arrayList = new ArrayList(Arrays.asList(stringArray));
String[] stringArr = new String[arrayList.size()];
arrayList.toArray(stringArr);
for (String s : stringArr)
System.out.println(s);
Convert an array to a set
Its Basically 2 step process.
Step 1 - Convert the Array to a List
Step 2 - Pass that list as input to declaration of Set
Set set = new HashSet(Arrays.asList(stringArray));
System.out.println(set);
//[d, e, b, c, a]
Copying parts of one Array to other Array
System.arraycopy(SourceArray,StartPointOfSourceArray,
DestinationArray,StartPointOfDestinationArray,
lengthOfCopying)
ArrayA = { 3, 5, 7,9};
ArrayB = { 10,20,30,40,50};
System.arraycopy(ArrayA,1,ArrayB,3,2);
Result : ArrayB = {10, 20, 30, 3, 5};
Some basic Validation for Array Input Questions (Applies to Most)
- Is the Array null ? -> (inputArray == null)
- Is the Array empty ? -> (inputArray.toString.isEmpty()) or (inputArray.length == 0)
- Does the Array have only one char ? -> (inputArray.length() == 1)
Two Dimensional Array
int[ ][ ] MultiDimArray = new int[4][3]; int [row][column]
[
[ 05 (Row = 0,Col = 0) , 10 (Row = 0,Col = 1) , 15 (Row = 0,Col = 2)]
[ 25 (Row = 1,Col = 0) , 30 (Row = 1,Col = 1) , 35 (Row = 1,Col = 2)]
[ 45 (Row = 2,Col = 0) , 50 (Row = 2,Col = 1) , 55 (Row = 2,Col = 2)]
[ 65 (Row = 3,Col = 0) , 70 (Row = 3,Col = 1) , 75 (Row = 3,Col = 2)]
]
Find the Number of Rows in the above = MultiDimArray.length
Find the Number of Column in the above = MultiDimArray[0].length
Different Type of Arrays Example
- Null Array [Use it for Error Check]
- Single Element Array Use it for Error Check]
- Sorted Array [Elements are in increasing order]
Binary Search will be used to find an element in the array -> O(long n) - Unsorted
- Array containing duplicate elements: [1,2,3,4,3,2]
- Few Elements but all of them are duplicate: [0,0,0,1,1,1,1,2,2]
- Rotated Sorted : [15,17,19,20,1,5,7,8,12]
- 0th element to kth Increasing and K+1th to nth decreasing: [1,5,8,9,3,2,0]