6.1 Array Creation and Access

MUST KNOW TERMS

  • Array: A data structure that holds a group of object references.
  • Element: A single item stored inside an array.
  • Element index: The location of an element in the array (in Java AND PYTHON(called a list) the first element is at index 0).
  • Array length: Number of elements in the Array

Declaring Array

To declare an array, you need the name of the array and the datatype.

Syntax: dataType[] arrayName;

(note in python that no need to declare which is called dynamic typing)

int[] grades; // Declare an integer array
String[] class_list; // Declare a string array
boolean[] light_switch;

Creating an Array

Gives memory for the array but we need to specify its size for java compared to python(with arraylists in the next lesson you won’t)


// Syntax: arrayName = new dataType[size];
int[]grades = new int[5]; // Create an integer array with 5 elements
String[]class_list = new String[3]; // Create a string array with 3 elements
boolean[]light_switch= new boolean[5];

// Can also predefine the terms rather than setting to default value
int[]grades={100,90,100,10,20};
boolean[]light_switch={true,false,true,false,true};


numbers=list()
names=list()
light_switch=list()

Printing out an Array

import java.util.Arrays;
// Printing out the array with java method
int[]grades={100,90,100,10,20};
System.out.println(Arrays.toString(grades));
[100, 90, 100, 10, 20]

Popcorn Hack 1(Time 0.5 minute)

Create an array that represents numerous car brands(Audi, toyota,etc. )

String[] brands = {"Porsche", "Toyota", "BMW", "Nissan", "Mclaren"};

Array with custom Objects

class Coder{
    public boolean codecodecoder;
    public int age;
    Coder(boolean codecodecoder, int age){
        this.codecodecoder=codecodecoder;
        this.age=age;
    }
    
}
Coder[]allthecoders=new Coder[5];


for(int i=0;i<allthecoders.length;i++){
    allthecoders[i]=new Coder(true,10);
}

System.out.println(Arrays.toString(allthecoders));
[REPL.$JShell$26$Coder@5a4be8a2, REPL.$JShell$26$Coder@548c1d9b, REPL.$JShell$26$Coder@50b383d2, REPL.$JShell$26$Coder@15b117a0, REPL.$JShell$26$Coder@10baf34c]

Accessing Array Elements

Acsessing an element’s value using it’s index in the array

int[] grades = {10, 20, 30, 40, 50};
int element = grades[1]; // Access the second grade (20) using index 2
System.out.println(element); // Output: 30
20
# Python translation/example
grades=[10,20,30,40,50]
student1_grade=grades[0] # the first element in the array/list
print(student1_grade)

10

Popcorn Hack #2(Time 1.5 Minutes)

  1. Make an array of chars, representing people’s grades
  2. Try to adjust the letter grade of at least 3 people
int[] scores = {90, 85, 92, 88, 91, 90, 76};

System.out.println(Arrays.toString(scores));

scores[0] = 85;
scores[1] = 90;
scores[5] = 78;

System.out.println(Arrays.toString(scores));

Array Length

Finding the length of the array.

int[] Students_commits = {10, 20, 30, 40, 50, 60, 70, 80, 90}; //number of commits for each student
int length = Students_commits.length; // Get the length of the array and assign to an integer variable
System.out.println("Array length and number of coders: " + length); // Output: Array length: 5
Array length and number of coders: 9
grades=[10,20,30,40,50,60,70,80,90]
length=len(grades)
print("number of students",length)
number of students 9

Modifying Array Elements

Updates the value of a specific element in the array.

// integer array

int[] grades = {10, 20, 30, 40, 50};
System.out.println("Before: "+Arrays.toString(grades));
grades[2] = 35; // Change the third grade to 35
System.out.println("After: "+Arrays.toString(grades));


// boolean array

boolean[]light_switch={true,false,true,false}; //array of light switches with true meaning the light is on and false meaning its off
System.out.println("Before: "+Arrays.toString(light_switch));
light_switch[0]=false; // turn off the first switch    true-->false
System.out.println("After: "+Arrays.toString(light_switch));

// String array
String[]student_names={"Bob", "Sir Mort"," Sir Tarun", "Sensei Wu"};
System.out.println("Before: "+Arrays.toString(student_names));
student_names[0]="Sir Jonathon";
System.out.println("After: "+Arrays.toString(student_names));


Before: [10, 20, 30, 40, 50]
After: [10, 20, 35, 40, 50]
Before: [true, false, true, false]
After: [false, false, true, false]
Before: [Bob, Sir Mort,  Sir Tarun, Sensei Wu]
After: [Sir Jonathon, Sir Mort,  Sir Tarun, Sensei Wu]

# Python translation/example
grades=[10,20,30,40]
print("Before",grades)

grades[1]=100 # changing the secon d element to 100
print("After",grades)
Before [10, 20, 30, 40]
After [10, 100, 30, 40]

Iterating Through an Array

Loops through the array, printing each element.

int[] numbers = {10, 20, 30, 40, 50};
for (int i = 0; i < numbers.length; i++) { // going from the first index(0) to the last index(length of array-1)
    System.out.println("Index "+i+": "+numbers[i]);
}
Index 0: 10
Index 1: 20
Index 2: 30
Index 3: 40
Index 4: 50
numbers=[10,20,30,40]
for i in range(0,len(numbers)):
    print("Index",i,":",numbers[i])
Index 0 : 10
Index 1 : 20
Index 2 : 30
Index 3 : 40