Arrays 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

ArrayList from an Array

A
  • String[] stringArray = { “a”, “b”, “c”, “d”, “e” };
  • ArrayList<string> arrayList = </string>

new ArrayList<string>(Arrays.asList(stringArray));</string>

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

Check if an Array contains a value

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

Array from ArrayList

A
  • String[] stringArray = { “a”, “b”, “c”, “d”, “e” };
  • ArrayList<string> arrayList = </string>

new ArrayList<string>(Arrays.asList(stringArray)); </string>

  • String[] stringArr = new String[arrayList.size()];
  • arrayList.toArray(stringArr);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

List from an Array

A
  • List<string> stooges = Arrays.asList("Larry", "Moe", "Curly");</string>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

fill(int[] a, int val)

A
  • Assigns the specified int value to each element of the specified array of ints.
  • fill(int[] a, int fromIndex, int toIndex, int val)
  • Assigns the specified int value to each element of the specified range of the specified array of ints. The range to be filled extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be filled is empty.)
  • Applies to long, short, char, byte, boolean, double, float and object arrays
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

binarySearch(int[] a, int key)

A
  • Searches the specified array of ints for the specified value using the binary search algorithm. The array must be sorted (as by the sort(int[]) method) prior to making this call.
  • Applies to long, short, char, byte, boolean, double, float and object arrays
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

copyOfRange(int[] original, int from, int to)

A
  • Copies the specified range of the specified array into a new array and returns it.
  • Applies to long, short, char, byte, boolean, double, float and object arrays
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

copyOf(int[] original, int newLength)

A
  • Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length
  • Returns: a copy of the original array, truncated or padded with zeros to obtain the specified length
  • Applies to long, short, char, byte, boolean, double, float and object arrays
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Arrays.sort() with comparator

A
  1. Arrays.sort(words, (a, b)->a.length() - b.length()); // words is string array
  2. This way we can even sort string arrays.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

sort(int[] a)

A
  • Sorts the specified array into ascending numerical order.
  • Applies to long, short, char, byte, boolean, double, float and object arrays
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

sort(int[] a, int fromIndex, int toIndex)

A
  • Sorts the specified range of the array into ascending order.
  • Applies to long, short, char, byte, boolean, double, float and object arrays
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

deepEquals(Object[] a1, Object[] a2)

A
  • Returns true if the two specified arrays are deeply equal to one another. Unlike the equals(Object[],Object[]) method, this method is appropriate for use with nested arrays of arbitrary depth.
  • Two array references are considered deeply equal if both are null, or if they refer to arrays that contain the same number of elements and all corresponding pairs of elements in the two arrays are deeply equal.
  • Two possibly null elements e1 and e2 are deeply equal if any of the following conditions hold:

e1 and e2 are both arrays of object reference types, and Arrays.deepEquals(e1, e2) would return true

e1 and e2 are arrays of the same primitive type, and the appropriate overloading of Arrays.equals(e1, e2) would return true.

e1 == e2

e1.equals(e2) would return true.

  • Note that this definition permits null elements at any depth.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Print an array

A
  • Array.toString(array);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly