unit 6 Flashcards
how do you create a new array?
first DECLARE
(int[] scores; OR all in one line)
then INITIALIZEEEEE
(int[] scores;
scores = new int[5];)
OR
int[] scores = new int[5];
this CREATES THE MEMORY LOCATION,
unlike just declaring it w/
int[] scores;
which does not do that
what are array values set at by default for all different data types?
int: 0
double: 0.0
boolean: false
string: null
can the amount of values that an array was created with be changed?
no
declare a new string array, names, with 10 values (but do not create one)
String[] names;
you cannot input a # of values when only declaring
create a new string array, names, with 10 values (having already been declared)
names = new String[10];
create a new string array, names, with 10 values, in one line
String[] names = new String[5];
do parameters edited by a method/within a specific scope stay changed after it ends?
no
do objects edited by a method/within a specific scope stay changed after it ends?
yes
objects get passed to a method by reference
arrays are immutable (true/false)
false
int x;
System.out.print(x);
will the above compile?
no, x is not initialized
true/false: in the statement
double x = Math.random()*10;
x is between the ranges of 1-10
no, it is 0-9. you must add one to make it one to ten.
what is a foreach loop?
loop that traverses each element of the array
describe the parts of a foreach loop header
for((datatype of array ) (user designated name) : (arrayname)
remember that THERE IS NOT ITERATOR in a foreach header, rather the user designated name is the value within the 1st, 2nd, etc. element of the array
foreach generally copies each element into a new variable of the same type like string methods (true/false)
true
what datatypes can a for each loop be used on ?
arrays (and strings, returning individual characters)
what are the TWO things wrong with the following code (assuming all variables to be properly declared and initialized):
for (int evenNumbers: values) { if (values[] % 2 == 0) { System.out.println(values[] + " is even!"); } }
the code used values as the “iterated” numbers, rather than the declared new variable declared int evenNumbers. also, even if it was values there should be no [] as the new variable is not an array.