JAVA ARRAYS

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

t

t or f An array can be also created by directly initializing it with data. Example: int[] arr = {1, 2, 3, 4, 5}; This statement declares and creates an array of integers with five elements, and initializes this with the values 1, 2, 3, 4, and 5.

multidimensional

_____ arrays are implemented as arrays of arrays.They are declared by appending the appropriate number of bracket pairs after the array name.

one

Accessing an element in a multidimensional array is the same as accessing elements in a ___ dimensional array

0, length

All array indices begin at __. The number of elements in an array is stored as part of the array object in the ___ attribute.

array

An ___ is a sequence of memory locations for storing data set out in such a way that any one of the individual locations can be accessed by quoting its index number.

elements

The individual items within an array are called ____.

ok

This is why we use arrays...type ok Suppose there are three variables of type int with different identifiers for each variable. Example: int number1; int number2; int number 3; number1 = 1; number2 = 2; number3 = 3; This seems like a tedious task in order to just initialize and use the variables especially if they are used for the same purpose.

index

To access an array element, or a part of the array, use a number called an ____ or a subscript.

t

t or f Coding Guidelines 1. It is better to create the array right away after you declare it. 2. The elements of an n-element array have indexes from 0 to n-1. Note that there is no array element arr[n]! This will result in an array-index-out-of-bounds exception. 3. You cannot resize an array. However, you can create a new array of a different size, copy the desired data into the new array, and assign the new array to the array variable.

t

t or f Creates an array of boolean variables with identifier results. This array contains 4 elements that are initialized to values {true, false, true, false}. oboolean[] results = {true, false, true, false};

t

t or f Creates an array of strings with identifier days and initialized. This array contains 7 elements. oString[] days = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};

t

t or f Initializing Array Variables To initialize an array, you use the new operator to allocate memory for objects. The new operator is followed by the data type with the number of elements to allocate specified. The number of elements to be allocated is placed within the [] operator. Example: name = new String[100]; age = new int[10];

t

t or f Like other variables, an array variable can be initialized when it is declared. ElementType[] arrayName = new ElementType[sizeOfArray]; or ElementType arrayName[] = new ElementType[sizeOfArray]; Example: int[] x = new int[10]; int[] ages = new int[100];

t

t or f Use the length attribute to iterate on an array as follows: int list[] = new int[10]; for(int i = 0; i < list. length; i++) { System.out.println(list[i]); } this prints out a vertical line of 10 (0's)

t

t or f index number or subscript o assigned to each member of the array, to allow the program to access an individual member of the array o an integer beginning at zero and progresses sequentially by whole numbers to the end of the array o Note: index is from 0 to (sizeOfArray-1)

t

t or f Creates an array of 4 double variables initialized to the values {100, 90, 80, 75}. odouble[] grades = {100, 90, 80, 75};

t

t or f Declaring Java Arrays To declare an array, write the data type, followed by a set of square brackets [], followed by the identifier name. ElementType[] arrayName; Example; int[] x; equivalently int x[]; int ages[]; equivalently int[] ages;

t

t or f examples of multidimensional arrays are //integer array 512 x 128 elements int[][] twoD = new int[512][128]; //character array 8 x 16 char[][] twoD = new char[8][16]; //String array 4 rows x 2 columns String[][] dogs = {{"terry", "brown"}, {"kristin", "white"}, {"toby", "gray"}, {"fido", "black"}


Ensembles d'études connexes

C2 OSI reference model (Lab & Review ?s)

View Set

World Religions Exam 1 Homework Questions

View Set

Chapter 41: Management of Patients with Musculoskeletal Disorders

View Set

EMT Geriatrics and Special Populations Practice Questions

View Set

NSG 245 Ch 41- Management of Musculoskeletal Disorders

View Set

Digital forensics chapters 1 and 4 review questions

View Set

Money and Banking Exam 1 Practice Test

View Set

Chapter 16: Care of Patients Experiencing Urgent Alterations in Health

View Set

MTH 115 - Review for Test 3 - Szalankiewicz

View Set