Array

Ace your homework & exams now with Quizwiz!

Use this to find the length of an array

a.length

Use this to find the length of a string

a.length()

when an array is passed as a parameter, it is possible to

alter the contents of the array

Sequence of values of the same type

array

Accessing a nonexistent element in an array results in

bounds error

How to grow an array that has ran out of space

create a new larger array, copy all elements into the array, store the reference to the new array in the array variable. double[] newData = new double[2*data.length]; System.arraycopy(data,0, newData, 0, data.length); data=newData;

Traversing an array with a regular for loop

double data[ ] = ...; for(int i=0; i<data.length; i++)

Declaration of an array variable

double[ ] data= new double[10];

Array length is

fixed (if you decide you need to add additional elements, then you need to make a new array)

The ArrayList class is a ______ class

generic; ArrayList<T> collects objects of type T

form a tabular, two dimensional arrangement with an index pair a[i][j]

two-dimensional arrays

How do you access array elements?

with an integer index using the notation a[ i ]

When you construct an ArrayList object, it has a size of?

0

How do you create an array of 5 ints? int[] arr = new int[6]; int[] arr = new int[5]; int arr = new int[5]; int arr = new int;

int[] arr = new int[5];

To set an array list element to a new value, use the

set method; For example, accounts.set(2, anAccount); sets position two of the accounts array list to anAccount, overwriting whatever value was there before

Sequence of values of the same type

Array

Example of array list

ArrayList<BankAccount> accounts = new ArrayList<BankAccount>(); accounts.add(new BankAccount(1001)); // The type ArrayList<BankAccount> denotes an array list of bank accounts. The angle brackets around BankAccount tell you that this is a type parameter//

You can collect a sequence of floating-point numbers in

ArrayList<Double>

Array

Arrays are ordered lists

If you know all the elements that you want to place in the array, you can create it by

int[ ] primes = {2, 3, 5, 7, 11}; (The Java compiler counts how many elements you want to place in an array, allocates an array of the correct size, and fills it with the elements that you specify)


Related study sets

Taxes: Chap 10 Partnership taxation

View Set

Six essential elements of geography

View Set

NTR 102 Chapter 19, alcohol, Gateway fall '18

View Set

Chapter 25: Negotiable Instruments

View Set

What kind of data is this? Level of Measurement

View Set