• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/35

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

35 Cards in this Set

  • Front
  • Back
How do you declare an array
Same as any object

- fartVolume = new double[80];
can you store the size of an array as a variable?
yes
int ARRAY_SIZE = 80;

- fartVolume = new double[ARRAY_SIZE];
how do you pronouce the following statement?

numbers[0] = 30
"numbers sub zero equals 30"
how do you store values into an array?
fartVolume[0] = 80;
fartVolume[1] = 71;

This stores the value 80 as the first value in the array because arrays start with a value of zero and 75 as the second value
how do you retrieve the value stored in an array?
fartVolume[0];

this is the same as the int value "80", it can be used the same way as a primative variable.
how can you used a for loop to enter multiple values in an array?

show proper syntax
int EMPLOYEE_NUM = 3;
int[] hours = new int[EMPLOYEE_NUM];

for (int index = 0; index < EMPLOYEE_NUM; index++){

hours[index] = keyboard.nextInt();

}
what is the off-by-one gotcha when dealing with array declaration
int EMPLOYEE_NUM = 3;
int[] hours = new int[EMPLOYEE_NUM];

for (int index = 1; index < 3; index++){

hours[index] = keyboard.nextInt();

} ** this will store the first entered value in the [1] position for the array, not the [0] position and it will terminate one assignment too much at [3]. the index needs to = 0 so that the loop terminates at the [2] position
can you declares a bunch of values in an array all at once?
yes

int[] fartVolume = {80, 71, 40};

or with strings
String[] stuff = new String[] {"three", "four" "five"};

in this case you do not need to specify how long the array is, it will automatically know based on the number of commas + 1. The values will be saved in the position they are declared
what is the difference between these two statements?

1.) int[] numbers;
2.) int numbers[];
3.) int[] numbers, scores, fartVolume;
4.) int numbers[], scores, fartVolume;
1.) numbers is declared as an int array
2.) numbers is declared as an int array
3.) all three are declared as int arrays
4.) numbers is declared as an int array, the other two are declared as primitive variable int's
what is the formula for an enhanced for loop?
for (datatype elementVariable: arrayName){

statments;

}
give an example of an enhanced for loop
for (int value : fartVolume){

system.out.println(value);

} ** this will print each value in the array list in order, provided that the values in the array are an int. the variable value will be assigned the value of the array during each iteration
show an for loop that uses the .length() method of the array as a parameter to terminate the loop
for (int index = 0; index < fartVolume.length(); index++){

statements;

} ** this will terminate the loop as soon as index reaches the number 1 less than the size of the array which is what we want since the 1st value of the array is 0.
how can you reference the same array with a different array name?

assume array1 is an existing array with 5 values stored in it
int array2[] = array1;

this copies the address of the first array and assigns it to the array2 variable. now both array1[] and array2[] reference the same array, thus if a change is made to one, it will be made to both. This usually is not desirable
How do you copy the contents of one array to another?

assume array1 is an existing array with an unknown number of values
int[] array2 = new int[array1.length()];

for (int index = 0; index < array1.length(); index++){

array2[index] = array[index];

} **this is a "deep copy" of array1 to array2, they are now two separate arrays stored in memory in two separate locations with the same information
You can pass array variables to methods
public int getArrayValue (int r){

return r;

} ** if you call this method inside of a loop it will show all the values in the array
you can pass the address of the array to a method as well
public void ArrayContents(int[] r){

for (int val : r){

System.out.println(val);

} ** this will display all the values of the array. Be sure not to change anything in the array during this method since r has direct access to the array

how to compare arrays (three steps)

assume array1 and array2 are existing int[] arrays with unknown values

Step 1determine if the length is equal
boolean arrayEqual = true;
if (array1.length != array2.length){
arrayEqual = false;
} ** first step is to compare the length

if (!arrayEqual){
System.out.println("The arrays have different lengths");
}
how to compare arrays (three steps)

assume array1 and array2 are existing int[] arrays with unknown values

Step 2 determine whether the contents are equal
int index = 0;

while (arrayEqual && index < array1.length){
if (array1[index] != array2[index]{
arrayEqual = false;
index++;
}
}
how to compare arrays (three steps)

assume array1 and array2 are existing int[] arrays with unknown values

Step 3 identify where the contents are not equal if that is the case
if (arrayEqual){
System.out.println("the arrays are equal")
} else {
system.out.println("the arrays are not equal in the " + index-1 + " position");
} ** this will tell you where the arrays are not equal. "index" was incremented in the last iteration that caused the loop to cancel due to the values being not equal. This is why you must subtract 1 from the index value to find the position that the arrays are not equal.
summing and averaging the values of an int[] array

can also be for any other type of array
int sum = 0;

for (int index; index < array1.length; index++){
sum += value;
}

double average = sum/(index+1);

** since index starts with 0, you need to add 1 to index so it truly represents the number of values thats being averaged

How do you pass an array to a method and store its contents in an instance field?




Do not allow changes to be made to the array that you are passing in

int[] classArray;




public void setValues(int[] array, int size){


classArray = new int[size];


for (int i=0; i < array.length; i++){


classArray[i] = array[i];


}


this.classArray = classArray;


}




**You must either know the size of the array you are passing and initialize it with a literal or allow the method to accept a variable that indicates its size


- you make a deep copy of the array ad store it as an instance variable so that its contents are not changed

Using the above method, how would you change the contents of the array you passed in?

Assume you've already initialized the array once with values and passed them to a setter method.
*classname = Poopfart*
public int[] getArray(){
int[] array = new int[classArray.length];
for (int i = 0; i < classArray.length; i++){
array[i] = classArray[i];
}
return array;
}

public static void main(String[] args){
Poopfart serious = new Poopfart();
array = serious.getArray();
}

this shows the second half of the process where the address of a new array is assigned to the 1st arrays variable name in the main method. this will take the place of the first array


How do you make an array of objects from a particular class?

ClassName[] arrayName = new ClassName[3];


for (int i = 0; i


arrayName[i] = new ClassName();


}




if you had 3 objects from the same class, you can store all three of them in here and still run operations on them individually or all together

How do you declare two dimensional arrays

int [][] twoDimArray = new int[3][4];




this means the two dimensonal array will have 3 rows (it will be 3 values high) and 4 columns (it will be 4 values wide)




rows run horizontally and stack high


columns run vertically and stack wide

say a student has 5 scores that make up his or her grade and there are 30 students in a class.




Does it matter if you make 30 columns and 5 rows for 30 rows and 5 columns?

yes. think of the students as the constant value and the tests a a variable. this is important for ragged two dimensional arrays.




you should have 30 rows, one for each student and 5 columns




then when you process all the values, the height (the array that holds the arrays of each students test scores) will always be the same and if a student missed a test, we can still calculate for it

How do you print out all the each students scores all at the same time provided that you organized the two dimensional array with 30 rows and 5 columns

int TESTS = 5;


int STUDENTS = 30;


int [][] class = new int[STUDENTS][TESTS]




for (int students = 0; students < class.length; tests++){


for (int tests=0; tests< class[student].length; test++){


System.out.println(class[STUDENTS][TESTS]);


}


System.out.println("\n");


}


this will cycle through all five tests, then execute a print line which will separate each student and then iterate again until all 30 students have been printed



how could you access a particular test score of a particular student?




1st test fifth student

class[0][5];




row 0, fifth column



how would you process the 2nd students grade for the semester?

for (int i = 0; i < TEST; i++){


double totPoints += class[i][1];


double average = totPoints / i;


}

how do you initialize a two dimensional array with values?

enclose each rows declaration list in its own set of braces separated by a comma




int[][] = {{1,2,3},


{2,4,9}};




this array has three rows and two columns

How do you get the length of a column?




how about a row?

class.length = height length (cannot change)




class[index].length = specific row length (this can change from one row to the next if its a ragged array so you would need to loop through the length to get an accurate answer.




Otherwise you could just multiply as follows:


class.length * class[0].length;

how to average columns in a multidimensional array ** provided that the array is not ragged**

for (int col=0;col < array[0].length; col++){


double total = 0;


for (int row=0; array.length; row++){


total += array[row][col];


}


average = total / array.length;


System.out.println(average);


}




prints the average of each row



How can you create a method that accepts any number of arguments of the same type? (part I)




say you want to sum the ints 3, 7, 9, 7, 7, 5

public int getSum(int... numbers){


int sum = 0;


for (int i = 0; i < number.length; i++){


sum += numbers[i];


}


return sum;




the "..." after the int declares an array that is initalized the same way as int[] = {3, 7, 9, 7, 7, 5}; and then passes that to the method so you can perform operations on it. It will work with all the same datatypes as arrays. You also treat it as its own parameter so it can accept

How can you create a method that accepts any number of arguments of the same type? (part II)




say you want to accept a bunch of strings and a bunch of doubles and one int

public void setValues(String... words, double... numbers, int number){


put statements here


}

Show a declaration of an ArrayList

ArrayList newArray = new ArrayList();




diamonds before parenthises




you can pass a size parameter into the () is you wish

what happens when you remove an item from an arraylist?




what about adding an item?

the item is deleted, all the indexes after the item are adjusted and the arraylist is shorter by 1




the exact opposite is the case if you insert an item at a specified location. otherwise the item is added to to the bottom