wisconsin
Methods and Arrays

Methods and Arrays

Curated by Last modified: 4 min read

Arrays in Java

Arrays are used to store multiple values in a single variable. They are used to store a collection of elements of the same type. An array can store a fixed-size sequential collection of elements of the same type. The elements in an array are stored in contiguous memory locations.

The syntax for defining an array of type in Java is:

type[] arrayName = new type[size];

Here, type is the type of elements that the array will store, arrayName is the name of the array, and size is the number of elements that the array can store.

Tip

The size of an array is fixed once it is created and cannot be changed.

To access a value, the syntax is:

arrayName[index];

Where index is the position of the element in the array (on a 0-based index).

Note

The time complexity of accessing an element in an array is .

Retrieving the length of an array is done using the length property:

arrayName.length;

Perfect Size Arrays

Arrays are perfect when you know the number of elements you want to store in advance. For example, if you want to store the grades of students in a class, you can create an array of size equal to the number of students in the class.

Tip

In CS 300, perfect size arrays are arrays that do not have a separate variable to store the size of the array. The size of the array is known at compile time. However, the array can contain the default value of the type of the array (null, , or false) and still be considered a perfect size array - as the data is not relevant to the size of the array.

Oversize Arrays

Oversize arrays are arrays that have a separate variable to store the size of the array. While the backing array may be larger than the number of elements stored in it, the size of the oversize array is not known at compile time. The size of the array is stored in a separate variable.

Array Compactness

An array is considered compact if the elements are stored in the first n positions of the array, where n is the number of elements in the array. The remaining positions in the array are not used. In other words, if there exists a default, uninitialized value in the array surrounded by initialized values, the array is not compact.

Example

The following array is not compact:

Object[] array = {1, 2, 3, null, 5};

The array is not compact because the value null is surrounded by initialized values.

The following arrays are compact:

Object[] array = {1, 2, 3, 4, 5};
Object[] anotherArray = {1, 2, null, null, null};

Methods in Java

Methods are blocks of code that perform a specific task. They are used to define the behavior of objects in a class. Methods are defined within a class and can be called to perform a specific action.

When using methods, it’s important to utilize them to organize code and make it more readable and maintainable. Methods can be used to break down complex tasks into smaller, more manageable pieces.

The syntax for defining a method in Java is:

accessModifier returnType methodName(parameterList) {
    // method body
}

Calling a method is done by using the method name followed by parentheses, with any required arguments inside the parentheses:

methodName(arguemnt1, argument2, ...);

Method Parameters

Parameters are variables that are used to pass values to a method. They are defined in the method signature and are used to provide input to the method. Parameters are enclosed in parentheses after the method name.

Primitive data types are simply passed by value, while objects are passed by reference. This means that changes made to an object passed as a parameter will affect the original object.

Source

The existing course note was retained and normalized during the merge.

Related: Topic index.

Linked to this page