Java Arrays

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/39

flashcard set

Earn XP

Description and Tags

Flashcards on Java Arrays

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

40 Terms

1
New cards

What is an array in Java?

A special variable having one name, but storing a list of data items, with each item being directly accessible.

2
New cards

What data types can an array store in Java?

int, double, char, boolean, String, or any other type, but all values must be the same type.

3
New cards

What is the syntax for creating an array in Java?

Base-type[] name = new Base-type[length];

4
New cards

What does the 'new' operator do when creating an array in Java?

It allocates memory for the array and automatically initializes all of its elements to zeros.

5
New cards

What is the significance of 'length' when creating an array?

The total number of elements in a given array.

6
New cards

What is the two-step process to create an array in Java?

baseType[] arrayName; arrayName = new baseType[length];

7
New cards

What should you keep in mind during the two-step array creation process?

You cannot use the array until you inform the compiler of the size of the array in step #2.

8
New cards

What exception is thrown if you try to create an array with a negative size?

A NegativeArraySizeException.

9
New cards

Is it possible to create an array with zero elements?

true

10
New cards

What is the syntax for initializing an array at declaration time in Java?

int[] a = {1, 2, 3, 4};

11
New cards

What is a partially-filled array?

Not all elements of the array might receive values.

12
New cards

How do you get the capacity of an array?

a.length

13
New cards

What is the capacity of an array?

The number of array elements that are created when you define the array.

14
New cards

What is the rule about the 'length' variable for a Java array?

It is final, its value cannot be changed.

15
New cards

In context of arrays, what is an 'index'?

The lighter numbers outside the boxes (usually start from 0).

16
New cards

What is the index of the first element in a Java array?

The first element of array starts at 0.

17
New cards

How to you access an element in a 1-D array?

Use two square brackets [ ] operator.

18
New cards

What exception occurs when the index is negative or greater than the array's length -1?

ArrayIndexOutOfBoundsException.

19
New cards

Give one example of 'Creating and Accessing Arrays'?

Read 7 or N temperatures from the user and show which are above and which are below the average of the N temperatures.

20
New cards

What is the formula of getting the last index?

Last index is arrayName.length – 1.

21
New cards

What is a two-dimensional array used for?

A data structure that can represent a table with rows and columns.

22
New cards

What is the Syntax for Declaring and Creating a Two-Dimensional Array in Java?

BaseType[][] arrayName = new BaseType[ROWS][COLUMNS];

23
New cards

What are 'Ragged Arrays'?

Not necessary for all rows to be of the same length.

24
New cards

How do you Access Elements in Two-Dimensional Arrays?

You could use two for loops, one nested within the other.

25
New cards

What happen while copying arrays?

Copies the reference, but it doesn’t copy the array itself.

26
New cards

What does Assignment operator '=' copies?

Reference or memory address.

27
New cards

How do we actually copy the array, not just the reference?

Create a new array then, copy the elements from one to the other

28
New cards

What is array traversal?

Looping through the elements of an array.

29
New cards

When we send to a method a single element or indexed variable as a method’s argument, it is also be called?

Pass by-value; where we just pass a copy of the arguments value to the parameter in the method’s header.

30
New cards

When we send to a method the entire array as a method’s argument, it is also be called?

Pass by-reference; where we actually pass the actual memory address of an array to the method.

31
New cards

What is the syntax difference writing for passing the entire arrays as arguments to a method?

No square brackets are written when you pass an entire array as an argument to a method.

32
New cards

Is array resizable?

Resizing an array is possible!

33
New cards

Returning array from methods, what is being returned?

When a method returns an array, the reference of the array is returned (memory address!)

34
New cards

What does method 'equals' do?

Checks length and each individual pair of array elements

35
New cards

To actually copy the array, not just the reference:

Create a new array.
Then, copy the elements from one to the other

36
New cards

What is an array?

Collection of variables of the same type.

37
New cards

How are arrays created?

Object, created with operator new.

38
New cards

How elements are numbered?

Starting with 0, ending with 1 less than the length.

39
New cards

What does an int do in a partially filled arrays?

Used to track how many are used

40
New cards

Multidimensional arrays are implemented as?

Array of arrays