Unit 6 Arrays
Unit 6 Arrays
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value
The length of an array is established when the array is created.
An array is a collection of similar types of data.
In the Java array, each memory location is associated with a number. The number is known as an array index.
Elements must be of the same type in a Java Array.
import java.util.*;
import java.io.*;
public class chooseMethod {
private int[] values;
public chooseMethod(int[] array) {
this.values = array;
}
public int[] getArray() {
return this.values;
}
public String getString() {
return Arrays.toString(this.values);
}
public void swap() {
int temp = values[0];
values[0] = values[values.length - 1];
values[values.length - 1] = temp;
}
// The line of code below replaces even values
public void replacement() {
for (int i = 0; i < values.length; i++) {
if (values[i] % 2 == 0) {
values[i] = 0;
}
}
}
public boolean incrementation() {
for (int i = 0; i < this.values.length - 1; i++) {
if (values[i] > values[i+1]) {
return false;
}
return true;
}
return false;
}
public static void main() {
chooseMethod method = new chooseMethod(new int[]{1,6,12,8,8,9,34,5,123,698,43,68,1});
System.out.println(method.getString());
System.out.println(method.incrementation());
method.swap();
System.out.println(method.getString());
method.replacement();
System.out.println(method.getString());
System.out.println(method.incrementation());
}
}
chooseMethod.main();