# Methods and Arrays

Source: https://wisconsin.twango.dev/fa24-cs300/u1-java-programming/methods-and-arrays

# Methods and Arrays

## 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:

```java
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:

```java
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 $O(1)$.
>

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

```java
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`, $0$, 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:
>
> ```java
> 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:
>
> ```java
> 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:

```java
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:

```java
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.

[0-based index]: https://en.wikipedia.org/wiki/Zero-based_numbering

## Source

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

Related: [[u1-java-programming/index|Topic index]].
