Java Arrays
Arrays:
int [] myIntArray = {7,3,6,3,1,2}; // Create a new array with values
System.out.println( myIntArray[0] ); // Access the first value (index 0)
myIntArray[2] = 5 ; // Modify the third value (index 2)
- An array is a collection of values which can be used like variables
- To access the value in an array, you put the array name followed by the slot position containing the value, where 0 is the first slot
- If you pass an array as a function parameter value, changes made to the array within the function will remain changed out of the function
Creating Empty Arrays:
int [] myIntArray = new int [7]; // Create a new array with 7 slots
- Be sure to set the values before you access them
Getting Length of Arrays:
int myLength = myIntArray.length;
- This is useful to know so you don't try to access something outside the bounds of the array
Challenge
Make an array of 10 strings and an array of 5 strings, write code that modifies the first item and displays the last item. Run the code once for each array.
Quiz
Completed