Array Methods Flashcards

1
Q

Declare an array

A

String[] aArray = new String[5];
String[] bArray = {“a”,”b”,”c”, “d”, “e”};
String[] cArray = new String[]{“a”,”b”,”c”,”d”,”e”};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Print an array

A
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]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Create an ArrayList from an array

A
String[] stringArray = { "a", "b", "c", "d", "e" };
ArrayList arrayList = new ArrayList(Arrays.asList(stringArray));
System.out.println(arrayList);
// [a, b, c, d, e]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Check if an array contains a certain value

A
String[] stringArray = { "a", "b", "c", "d", "e" };
boolean b = Arrays.asList(stringArray).contains("a");
System.out.println(b);
// true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Find the Middle Element of the Array

A

int Start = 0 ;
int End = InputArray.length - 1
int Middle = Start + (End - Start)/2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Declare an array inline

A

method(new String[]{“a”, “b”, “c”, “d”, “e”});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Joins the elements of the provided array into a single String

A
// 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Convert an ArrayList to an array

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Convert an array to a set

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Copying parts of one Array to other Array

A

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};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Some basic Validation for Array Input Questions (Applies to Most)

A
  1. Is the Array null ? -> (inputArray == null)
  2. Is the Array empty ? -> (inputArray.toString.isEmpty()) or (inputArray.length == 0)
  3. Does the Array have only one char ? -> (inputArray.length() == 1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Two Dimensional Array

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Different Type of Arrays Example

A
  1. Null Array [Use it for Error Check]
  2. Single Element Array Use it for Error Check]
  3. Sorted Array [Elements are in increasing order]
    Binary Search will be used to find an element in the array -> O(long n)
  4. Unsorted
  5. Array containing duplicate elements: [1,2,3,4,3,2]
  6. Few Elements but all of them are duplicate: [0,0,0,1,1,1,1,2,2]
  7. Rotated Sorted : [15,17,19,20,1,5,7,8,12]
  8. 0th element to kth Increasing and K+1th to nth decreasing: [1,5,8,9,3,2,0]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly