CSCI 1301 Exam 3
how can you sum up all the elements of an array?
double total = 0; for (int i = 0; i < arrayName.length; i++) { total += arrayName[i]; }
example of "array initializer"
double[] arrayName = {1.2, 2.5, 3.7};
What is another way to do the previous problem and example using more lines?
double[] myList = new double[4]; myList[0] = 1.9 myList[1] = 2.9 myList[2] = 3.4 myList[3] = 3.5
example of declaring, creating, and initializing in one step
double[] myList = {1.9, 2.9, 3.4, 3.5};
what are the values in an array normally called
elements
what does a selection sort do?
finds the smallest number in the list and places it first, it keeps doing this
print an array using a for loop
for (int i = 0; i < arrayName.length; i++) { System.out.println(arrayName[i]); }
once an array is created, can its size be changes?
nope, the size is fixed
what does it mean for the indices to be "0-based"?
starts at 0 and goes up to arrayRefVar.length - 1
advantages and disadvantages of using a variable number of arguments?
the "array" can be any size that isn't fixed, so you can keep adding values however, the elements aren't actually stored, so you can't do many individual things with the elements
what is searching an array?
the process of searching for a specific element in an array ex: discovering whether a specific score is included in a list of scores
Arrays.toString(list)
this method can be used to return a string representation for the list
what is garbage?
unreferenced memory
what is the enhanced for loop/for each loop used to do?
used to iterate through elements of arrays and collections
how can methods accept a variable number of arguments of the same type?
using an ellipsis (...) in the parameter. It will treat it as an array inside of the method example: sum (3, 5, 8, 2, 1); // calling the method public static int sum(int...num) // method header
After an array is created, can an indexed variable be used in the same way as a regular variable?
yes example: myList[2] = myList[0] + myList[1];
How can you declare and create an array in one step?
datatype[] arrayRefVar = new datatype[arraySize] example: double[] myList = new double[10]
How do you declare array variables?
datatype[] arrayRefVar; example: double[] mylist
what does merge sort do?
It divides the input array into two halves, calls itself for the two halves, and then merges the two sorted halves.
How can I access the position of the last element?
myList[myList.length - 1] = 13
when created, array elements are assigned the default values of:
- 0 for numeric primitive data types - '\u0000' for char types - false for boolean types
benefits of using a regular for loop over the enhanced for loop while working with arrays?
- you can go in different order - you can do anything that requires access to another index other than the current - you can change the elements in an array
what are some sorting algorithms?
-bubble sort -selection sort -insertion sort -merge sort
what are the 3 proper methods for copying arrays?
1. using a loop (for loop) 2. the arraycopy() method 3. the clone() method [learn in csci 1302]
What is garbage collection?
Garbage collection is a process of reclaiming the runtime unused objects.It is performed for memory management. It is done by JVM
What does a bubble sort do?
It works by going through each number individually and assessing whether or not the number on the left is bigger or smaller, if it is bigger the two numbers switch place.
is java pass by value or pass by reference?
Java is strictly pass by value.
where does JVM store the array in memory?
an area in memory called a heap, , which is used for dynamic memory allocation where blocks of memory are allocated and freed in an arbitrary order
what is an anonymous array?
array with no named reference variable
what is the index of the first element in an array?
arrayName[0]
How can you find the size or length of an array?
arrayRefVar.length example: myList.length returns 10
indices go from 0 to what?
arrayRefVar.length - 1 so an array with length 10 goes from 0-9
How do you create an array?
arrayVar = new datatype[arraySize]; example: myList = new double[10]
What is an array
data structure that represents a collection of the same types of data
enhanced for loop syntax
dataType variableName : arrayName example of enhanced for loop to print array elements: double num : numbers System.out.println(num);
java.util.Arrays class
has array operations for convenience
Caution: when using shorthand notation, the declaration, initialization, and creation of an array must happen in how many statements?
in ONE statement. otherwise, a syntax error would occur
what is the difference between enhanced for loop and a regular for loop?
in the enhanced for loop you cant go backwards and you also can't stop at a specific index. it goes through the array from the beginning and all the way through
array elements are accessed through the ____
index
in what syntax are each element in an array represented?
indexed variable, which is arrayRefVar[index]; example: myList[0] for the first
what does pass by value mean
means the called functions' parameter will be a copy of the callers' passed argument. so changing the value of the local parameter inside the method does not affect the value of the variable outside the method