Please enable JavaScript.
Coggle requires JavaScript to display documents.
Using the Debugger & Arrays (array functions (to fill an array (int[]…
Using the Debugger & Arrays
your debugger allows you to set break points
break points - program will run until it hits break point and then it will stop
go to run tab and toggle break point
arrays allow you to have multiple data types
to initialize
double[] values;
double[] values = new values[10];
you cannot use the array until you set the size of the array
all vals here are set to 0
indexes are 0-9
int[] primes = {2,3,4,5,6};
to initialize a element
values[4] = 35
remembers, ARRAYS ARE ZERO BASED
you can have primitive and obj vars in here
tips
by reference or value
for primitives
x=y; does not modify x and/or y
by value
for objects,class, string, arrays
x=y; does modify x and/or y
by reference
this does not make a copy of x and assign it to y. They both take on the same location & the changes made to x/y affect y/x
you cannot resize arrays in java easily
use a final var to initialize size
there is an example in the book (maybe try to example)
the enhanced for loop
basically say "for each element in the array"
the loop will access every element in the array
executes loop body
it copies it to the element variable
for (double element: values) {total = total+ element;}
this is the same as for (int i = o; i < values.length; i++){total += values [i] }
element is the elements in the array, values is the array, statements in the loop body are executed for every element
differences: runs through EVERY element, can't change any elements, does not include indexes, will always run through the whole array - does not work for partially empty arrays
array functions
to fill an array
int[] values = new int [11];
for (int i = 0; i < values.length; i++){values[i]=i*i;}
sum and avg of an array
for(double elements: values){total = total+element;} if (values.length > 0) {avg = total/values.length;}
min&max
set largest to first element, use for loop, set any num larger than first el = to largest, use same logic for minimum
turn an array into a sting
Arrays.toString(arrayName); need to import java.util.*; for this one
Searches - finding a specific val in an array & Removal
linear search
example in book
how do you delete an element in an array?
values[pos[ = values[currentSize-1]; currentSize--;