arrays

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

swapping elements

Use a temporary variable when swapping two elements. double temp = values[i]; values[i] = values[j]; values[j] = temp;

copying arrays

Use the Arrays.copyOf method to copy the elements of an array into a new array. If you want to make a true copy of an array, call the Arrays.copyOf method. double[ ] prices = Arrays.copyOf(values, values.length)

adding to an array list

arr.add("Something") adds it to the end USE SET to set an index to a specific value, do arr.set(2, "Something) arr.add(2,"Something") would add it and shift all the ones after it to remove, do arr.remove( )

get the length of an array list

arr.size( )

determining the number of elements in an array vs array list

array: arr.length array list: arr.size( )

declaring and initializing an array

declare: double[ ] values; initialize: double[ ] values = new double[10]; specify initial values: double[ ] moreValues = { 32, 54, 67.5, 29, 35, 80, 115, 44.5, 100, 65 };

Max and min of an array

double largest = values[0]; for (int i = 1; i < values.length; i++) { if (values[i] > largest) { largest = values[i]; } }

how to sum and average an array

double total = 0; for (double element : values) { total = total + element; } double average = 0; if (values.length > 0) { average = total / values.length; }

copying an array and then doubling the length of it

double[ ] newValues = Arrays.copyOf(values, 2 * values.length);

declaring a 2-dimensional array

final int COUNTRIES = 8; final int MEDALS = 3; int[ ] [ ] counts = new int [COUNTRIES][MEDALS]; [rows][columns] or separate row items with {1,0} and start a new row by putting commas in between {}

how to access all elements in a 2D array

nested loops - rows then columns for (int i = 0; i < counts.length (which is rows); i++) { for (int j = 0; j < counts[0].length(which is columns); j++) { System.out.printf("%8d", counts[i][j]); } System.out.println( ); }

can you do length++ to add an element?

no! that's a compiling error

length of an array

number of elements it has

bounds error

occurs if you supply an invalid array index, can cause your program to terminate.

shuffle thing

public static void q1(int[] values) { int temp = values[0]; for(int i = 1; i < values.length; i++) { value s[i-1] = values[i]; } values[values.length-1] = temp; }

how to remove "matches" from an array list

use a while loop and an if statement. To remove words that have a length less than 4, use this int i = 0; while (i < words.size()) { String word = words.get(i); if (word.length() < 4) { words.remove(i); } else { i++; } }

****how to convert a normal array to an array list

using the array list methods instead of the array syntax (see Table 3). For example, this code snippet finds the largest element in an array: double largest = values[0]; for (int i = 1; i < values.length; i++) { if (values[i] > largest) { largest = values[i]; } }

wrapper class

A class that contains a primitive type value, such as Integer. To collect numbers in array lists, you must use wrapper classes.

Enhanced for loop

A special type of for loop that traverses an array or array list from beginning to end, binding a local variable to each element in the array or list. Use the enhanced for loop if you do not need the index values in the loop body (if you're moving values or anything like that, enhanced for loop is not the way to go) for (double element : values) means "for each element in values"

what is an array?

An array collects a sequence of values of the same type.

array lists

An array list stores a sequence of values whose size can change. • Array lists can grow and shrink as needed. • The ArrayList class supplies methods for common tasks, such as inserting and removing elements.

using an enhanced for loop with an ArrayList

ArrayList<String> names = . . . ; for (String name : names) { System.out.println(name); } equivalent to for (int i = 0; i < names.size(); i++) { String name = names.get(i); System.out.println(name); }

declaring an array list

ArrayList<String> names = new ArrayList<String>( ); MUST include the type in < >

make a copy of an array list

ArrayList<String> newNames = new ArrayList<String>(names); put name of copied array in the parentheses

auto-boxing

Automatically converting a primitive type value into a wrapper type object. Because boxing and unboxing is automatic, you don't need to think about it. Simply remember to use the wrapper type when you declare array lists of numbers. From then on, use the primitive type and rely on auto-boxing.

removing an element

If the elements in the array are not in any particular order, simply overwrite the element to be removed with the last element of the array, then decrement the currentSize variable. values[pos] = values[currentSize - 1]; currentSize--; If order matters, then move all the element after the one you want to remove to a lower index and then decrement the variable holding the size of the array for (int i = pos + 1; i < currentSize; i++) { values[i - 1] = values[i]; } currentSize--;

inserting an element

If the order of the elements does not matter, you can simply insert new elements at the end, incrementing the variable tracking the size. if (currentSize < values.length) { currentSize++; values[currentSize - 1] = newElement; } to add one at a particular position in the middle of the array, move all elements after the insertion to.a higher index and then insert the new element if (currentSize < values.length) { currentSize++; for (int i = currentSize - 1; i > pos; i--) { values[i] = values[i - 1]; } values[pos] = newElement; } Before inserting an element, move elements to the end of the array starting with the last one Note the order of the movement: When you remove an element, you first move the next element to a lower index, then the one after that, until you finally get to the end of the array. When you insert an element, you start at the end of the array, move that element to a higher index, then move the one before that, and so on until you finally get to the insertion location.

accessing elements in a 2D array

Individual elements in a two-dimensional array are accessed by using two index values, array[i][j].

how to fill an array with squares

for (int i = 0; i < values.length; i++) { values[i] = i * i; }

what do you need to import to use arrays in a class

import java.util.Arrays;

how to create a 2D array with rows of different lengths

look at special topic 7.3


संबंधित स्टडी सेट्स

Chapter 12 Section 1 Social Studies

View Set

College History Final Review Questions

View Set

Exam 3 - Care of Adult - Ch. 38, 41, 42, 43

View Set

NR 226 Exam 1 - Review Questions

View Set

ISSA PERSONAL TRAINER CHAPTER 10- CONCEPTS OF FLEXIBILITY TRAINING

View Set

LAT: Operation Wetback - 1953-1959

View Set

Intermediate Accounting II EXAM 4 Smartbook

View Set

CompTIA Security+ (SY0-601) Practice Exam #6

View Set