Language Basics: Variables: Arrays Flashcards

1
Q

What is an array?

A

A container object that holds a fixed number of values of a single type.

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

What is the length of an array?

A

The length of an array is established when the array is created. After creation, its length is fixed.

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

What is an element and index in an array?

A

Each item in an array is called an element, and each element is accessed by its numerical index.

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

How do you declare an array?

A

int[] anArray;

an array declaration has two components: the array’s type and the array’s name. An array’s type is written as type[], where type is the data type of the contained elements; the brackets are special symbols indicating that this variable holds an array.

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

How do you initialise an array?

A

anArray = new int[10];
If this statement is missing, then the compiler prints an error like the following, and compilation fails:

ArrayDemo.java:4: Variable anArray may not have been initialized.

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

How do you initialise an array?

A

anArray = new int[10];
If this statement is missing, then the compiler prints an error like the following, and compilation fails:

ArrayDemo.java:4: Variable anArray may not have been initialised.

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

What is a multidimensional array

A

A multidimensional array is an array whose components are themselves arrays. This is unlike arrays in C or Fortran. A consequence of this is that the rows are allowed to vary in length

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

How do you find length of an array?

A

use the built-in length property to determine the size of any array.

System.out.println(anArray.length);

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

How do you copy an array?

A

The System class has an arraycopy method that you can use to efficiently copy data from one array into another:

public static void arraycopy(Object src, int srcPos,
Object dest, int destPos, int length)
The two Object arguments specify the array to copy from and the array to copy to. The three int arguments specify the starting position in the source array, the starting position in the destination array, and the number of array elements to copy.

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

What useful methods does the java.util.Arrays class have?

A
  1. Searching an array for a specific value to get the index at which it is placed (the binarySearch method).
  2. Comparing two arrays to determine if they are equal or not (the equals method).
  3. Filling an array to place a specific value at each index (the fill method).
  4. Sorting an array into ascending order. This can be done either sequentially, using the sort method, or concurrently, using the parallelSort method introduced in Java SE 8. Parallel sorting of large arrays on multiprocessor systems is faster than sequential array sorting.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly