Arrays Flashcards
What are arrays?
A type of object in Java used to store many values in one variable. Created to hold a specific number of values (any non-negative integer)
What is the length of an array?
An array always knows how many values it contains: its length. The length can never change.
What is an element?
An individual item stored in an array
What is a subscript?
aka an index: each element has an index/subscript. The subscripts are always from 0 to length-1
How do you create an array?
There are two parts:
1. declare the reference variable for the array. like int[] someInts;
2. instantiate the array and assign it to the reference variable. like someInts = new int[7];
What are some things to be aware of when declaring an array?
- there is never a number inside the []
- This type can hold a reference to any array of that type of data
- declaring an array does not create an array object
- at first, the variable contains null
What are some things to be aware of when instantiating an array?
- must use the ‘new’ keyword
- this allocates memory for an array object that can hold a specific number of elements
- instantiation gives you a reference to the newborn array
- the length can never change but the elements can change
What does it mean for an array to be an Object?
- array reference variables are stored on the stack, array objects are stored on the heap
- the stack contains all primitive types and references to other data
- the heap contains all non-primitive data
What does the ‘null’ reference mean?
“no object”. A new reference variable will always contain null if you don’t create an object and assign the object to the reference variable. You can set object reference variables to null, meaning that there is no address stored and the variable does not point to an object on the heap
What are some things to keep in mind when literally instantiating an array?
- Literals do not require the new keyword to create the array but you can use it
- if you are using literal initialization but it’s declared on a different line you need the new
How can you access the length of the array?
using myData.length. There is no (). You cannot change the length, only find its value
How can you access the elements of the array?
Using myData[expression]; The expression must result in an integer from 0 to myData.length-1. You can freely access or change the elements anywhere you can use any other variable of that type
How must you go about copying an array?
If you have two array variables of the same type, you can use assignment to ‘copy’, but this will be a shallow copy as it just copies the reference. For a deep copy that creates a new separate array you have to instantiate a new array of the same size, and loop through the original and copy each element into the second array
What gets passed or returns when passing arrays?
Only the reference. The function gets a shallow copy. That means that any changes made to the elements of an array parameter inside a function will affect the original. However, if you overwrite the array parameter, it will now point to a new array and not change the address in the original array
What are aliases?
Two array variables that point to the exact same array object (have the same reference)