Single-Dimensional Arrays Flashcards Preview

► Java > Single-Dimensional Arrays > Flashcards

Flashcards in Single-Dimensional Arrays Deck (10)
Loading flashcards...
1
Q

For finding the length of an array, should you use arrayVar.length() or arrayVar.length? What about strings? stringVar.length() or stringVar.length?

A

.length for arrays, .length() for strings

2
Q

What method can you use to sort arrays?

A

java.util.Arrays.sort(myArray). This method doesn’t return anything, it just sorts the given array.

3
Q

What method can you use to search for a key in an array?

A

you can use java.util.Arrays.binarySearch(myArray, key). The array must be pre-sorted in ascending order before using binarySearch. If key is present, the method returns the index of the key. If not present, method returns -(insertionindex + 1)

4
Q

How can you compare two arrays to check if they’re the same?

A

Use the method java.util.Arrays.equals(list1, list2). If the lists have the same content and in the same order, the method returns true. If same content, but not same order, returns false.

5
Q

What does the method java.util.Arrays.fill do?

A

java. util.Arrays.fill(list1, x) fills the whole array with whatever value is in x, and overrides whatever was there before. Optionally you can fill only a part of the array:
java. util.Arrays.fill(list1, startIndex, endIndex, x)

6
Q

What does the java.util.Arrays.toString(myArray) method do?

A

It returns the content of the list as one whole string, including square brackets at start and end.

7
Q

binary search, linear search, selection sort, insertion sort

A

study these….

8
Q

What’s the difference between changing arrays and non-array-arguments inside methods?

A

Changing a passed-in argument inside a method does not change the object outside the method, but changing a passed-in array will also change the array outside the method

9
Q

Which of the following is incorrect?

A. int[] a = new int[2];
B. int a[] = new int[2];
C. int[] a = new int(2);
D. int a = new int[2];
E. int a() = new int[2];
A

C, D and E are incorrect

10
Q

What is the correct term for numbers[99]?

A. index
B. index variable
C. indexed variable
D. array variable
E. array
A

C. indexed variable