Skip to content
Ronald Lencevičius edited this page Jan 5, 2018 · 20 revisions

Overview

In this section we'll be going over using and processing arrays. The array class is a powerful tool that allows us to store multiple values at once and as a result, allows us to return more than one value outside of a method for use.

New Concepts

Declaring arrays

An array is a group of like-typed variables/values that are referred to by a common name. Array can contains primitives data types, like integers and strings, as well as objects of a class depending on the definition of array.

An array declaration has two components: the type and the name. Type declares what kind of things the array will hold.

public class Main{
   int[] numbers = ....; //the type is int and the name of the array is numbers
   ...
   string[] names = ...; //the type is string and the name of the array is names
}

To instantiate an array, we also need to specify the size of the array. So as a recap:

First, we must declare a variable of the desired array type.

Second, we must allocate the memory that will hold the array, using new, and assign it to the array variable.

public class Main{
   int[] numbers = new int[20]; //this int array has a size of 20, meaning it can hold 20 integers
}

In a situation, where the size of the array and elements of the array are already known, array literals can be used. We essentially list out the array elements without specifying the size

public class Main{
   int[] numbers = {1,2,3,4,5,6,7,8,9,10}; //this int array has a size of 10, an the elements are 1-10
}

Accessing/Editing elements

In the section above we learned how to declare an array with a specific size or with specific elements inside of it. But how do we populate the empty array? How do we change an already stored value within it?

In order to access an element from a declared array, you must use the array name with the position of the value you want to access:

> int[] arr = {1, 2, 3}; //array size 3, name arr
> int c = arr[0]; //storing 1 into variable c
> System.out.println(c);

1

One very important note is that array positions start at zero. Meaning if you have int[] arr = new int[10], the size of the array is 10 with the positions for accessing elements being 0-9.

If you tried to access the 10th element like so: arr[10] = ... an ArrayIndexOutOfBoundsException will be thrown.

Here is another example of populating an array using a for loop and then changing a value.

> public class Example {
>    public static void main(String[] args) {
>       int[] arr = new int[10];
>       
>       for(int i = 0; i < arr.length; i++) {
>          arr[i] = i + 1; //populating the array with values 1-10
>       }
>
>       System.out.println("Stored value in position 3: " + arr[2]);
>
>       arr[2] = 47; // Assigning new value to position 3
>
>       System.out.println("Stored value in position 3: " + arr[2]);
>    }
> }

Stored value in position 3: 3
Stored value in position 3: 47

Foreach loop

Cloning/Copying

Passing as arguments

Returning arrays

Activity

Program that asks for user to input data that is then sorted, outputted, and shows the min, the max, the mean, and standard deviation of the set.

Summary

Practice Problems

Clone this wiki locally