Skip to the content.
Intro Primitive Types Reference Types Stack and Heap Code Example Quiz

Team Teach for Unit 6 Arrays

Arrays

CSA-Quiz

6.1: Array Creation and Access

Intro to Arrays

What are arrays?

They are data structures used to implement a collection of primitive or object reference data. An array in Java is basically like a list in python, but they are only allowed to store values of one data type.

Each value in the array is called an “element”

The position of an element in the array is called an “index”. Just like in Python, the first element of an array always has an index of zero.

The length of an array is how many elements there are in the array.

length has the dataype public final

  • we can access the length in any class since it’s public
  • also, we can’t change the array’s length after it’s been created since the length is final

Example

int [] array1 = new int[5]

The above code cell defines the array with:

  • the name array1
  • the datatype int (means that only integer values can go in the array)
  • the number of elements in the array is five, as shown by int[5]
  • all the 5 elements in the array have a value of zero, because the elements weren’t specified

Question: How would you find the index of the last element of the array, if you didn’t know how many elements there were in the array?

You guys answer!!

Remember, unlike python, negative indexing does not work in Java and it will result in an error !!!

Uncomment the code and see for yourself!

// int [] array2 = {1,2,3,4,5};
// System.out.println(array2[-1]);

Primitive Elements

Now, let’s go back to array1 and modify some stuff.

System.out.println(array1[4]); // 5th element of the array
0

As you can see, it returns zero because when we defined the array, we used new, which made all the elements have the value of zero. Now, let’s change it.

array1[4] = 30;
System.out.println(array1[4]);
30

We successfully changed the fifth element from zero to thirty.

Putting the name of the array in the print statement in Java outputs the memory reference of the array and not the elements of the array. Below is a great way to print out the array.

System.out.println(array1)
[I@536093be
System.out.println(Arrays.toString(array1));
[0, 0, 0, 0, 30]

Popcorn Hack (do this on separate cells)

  • Create an array called “city_array” with the elements “San Diego”, “Los Angeles”, “San Francisco”, “Sacramento”
  • Print out the first element in the array
  • Print out the third element in the array
  • Change the second element in the array to “Sacramento”
  • Change the fourth element in the array to “San Jose”
  • Print out the length of the array

Mutable Elements

public class Mutable{
    private int value;
    public Mutable(int start) {
        value = start;
    }
    public void incrementValue() {
        value++;
    }
    public int getValue() {
        return value;
    }
}
Mutable[] array3; //initialize array of mutable objects
array3 = new Mutable[3]; //set size of array3 equal to three
array3[1] = new Mutable(33); //assigning the number 33 to the second element
array3[0] = new Mutable(array3[1].getValue() - 3); //assigning the second element's value minus three to the first element aka the number 30
for (Mutable m : array3) { //printing out the values of the array using for loop
    if (m != null) {
        System.out.println(m.getValue());
    } else {
        System.out.println("null");
    }
}
30
33
null

Important: When the array is initialized (you’re not providing any values to it):

  • int elements are set to 0
  • reference type elements are set to null
  • double elements are set to 0.0
  • boolean elements are set to false

Popcorn Hack:

Create four new INITIALIZED arrays, with int, string, double, and boolean types respectively. Print out the second element of each array to see the default values.

Practice MCQs

Question 1

Consider the following method which is intended to return the position of find within the String referenced at the third last index of arr.

public static int findThirdLast(String [] arr, String find)
{
return /*missing code*/;
}

Which of the following could replace /*missing code*/ to complete the method as specified?
A. arr[].indexOf(find)
B. arr.indexOf(find)
C. arr[arr.length].indexOf(find)
D. arr[arr.length - 3].indexOf(find)
E. arr[arr.length - 2].indexOf(find)

Here’s an image for your reference, in case you want to visualize it.

public static int findThirdLast(String [] arrtest, String find)
{
    return arrtest[arrtest.length-3].indexOf(find);
}
String [] testArray = {"cat", "dog", "horse", "monkey", "snake", "elephant"};
int result = findThirdLast(testArray, "on");
System.out.println(result);
1

Question 2

Consider the following method:

public static int mystery(int [] arr)
{
return arr[1] + arr[4]/2
}

The mystery method is called from another method in the same class: int[] list = {1,9,2,5,6};
int result = mystery(list);

What is stored in result after executing the above code?

A. 2
B. 12
C. 15
D. 9
E. 8

public static int mystery(int [] myarr)
{
    return myarr[1] + myarr[4]/2;
}
int[] list = {1,9,2,5,6};
int result = mystery(list);
System.out.println(result);
12