Arrays
Seoul National University of Science and Technology
Information Technology Management
Lecture slides index
May 7, 2025
ArrayList, which has a lot of functionality to make the life of a programmer easier.ArrayList is unlimited.ArrayList – they have been programmed like any other programs or tools offered by the programming language.ArrayList runs out of space, a larger space is reserved and the data from the previous space is copied to the new one.ArrayList is simple to use, sometimes we need the ancestor of the ArrayList, the Array.new followed by the type of the elements, square brackets and the number of the elements in the square brackets.int[] numbers = new int[3];
numbers[0] = 2;
numbers[2] = 5;
System.out.println(numbers[0]);
System.out.println(numbers[2]);get-method works very similarly to accessing an element of an Array.Scanner reader = new Scanner(System.in);
int[] numbers = new int[5];
numbers[0] = 42;
numbers[1] = 13;
numbers[2] = 12;
numbers[3] = 7;
numbers[4] = 1;
System.out.println("Which index should we access?");
int index = Integer.valueOf(reader.nextLine());
int number = numbers[index];
System.out.println(number);int[] numbers= new int[5];
numbers[0] = 42;
numbers[1] = 13;
numbers[2] = 12;
numbers[3] = 7;
numbers[4] = 1;
// code herenumbers[0]=numbers[1];numbers[1]=numbers[0];numbers[0]=numbers[1];int helper = numbers[0];numbers[0]=numbers[1];numbers[1]=helper;numbers[1]=numbers[0];int helper = numbers[0];numbers[1]=numbers[0];numbers[0]=helper;import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] array = new int[5];
array[0] = 1;
array[1] = 3;
array[2] = 5;
array[3] = 7;
array[4] = 9;
int index = 0;
while (index < array.length) {
System.out.println(array[index]);
index++;
}
System.out.println("");
// Implement here
// asking for the two indices
// and then swapping them
System.out.println("");
index = 0;
while (index < array.length) {
System.out.println(array[index]);
index++;
}
}
}length.numbers.length.numbers.length() doesn’t work.int[] numbers = new int[4];
numbers[0] = 42;
numbers[1] = 13;
numbers[2] = 12;
numbers[3] = 7;
System.out.println("The array has " + numbers.length + " elements.");
int index = 0;
while (index < numbers.length) {
System.out.println(numbers[index]);
index = index + 1;
}Output:
numbers[0], then numbers[1] etc.index < number.length is false, i.e.import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] array = new int[10];
array[0] = 6;
array[1] = 2;
array[2] = 8;
array[3] = 1;
array[4] = 3;
array[5] = 0;
array[6] = 9;
array[7] = 7;
System.out.print("Search for? ");
int searching = Integer.valueOf(scanner.nextLine());
// Implement the search functionality here
}
}System.out.print("How many numbers? ");
int howMany = Integer.valueOf(reader.nextLine());
int[] numbers = new int[howMany];
System.out.println("Enter the numbers:");
int index = 0;
while (index < numbers.length) {
numbers[index] = Integer.valueOf(reader.nextLine());
index = index + 1;
}
System.out.println("Here are the numbers again:");
index = 0;
while (index < numbers.length) {
System.out.println(numbers[index]);
index = index + 1;
}array[0] we’re referring to the first element of the array.array[0] can also be read “Go to the beginning of the array and move forward 0 times the size of the variable contained in the array – and return a chunk of data the size of the variable.
int variable in java is 32 bits.int is 231-1.int array of 4 elements, 4 * 32 bits of memory is allocated to hold the integers.array[2], 32 bits are read starting from beginning of the array + 2 * 32 bits.array[-1], we would find the data located just before the array in the memory of the program.Output
integerArray, meanwhile the caller of the method has named the same array numbers.Main has a method public static int sumOfNumbersInArray(int[] array).public static void printNeatly(int[] array) in the class named Main to make it print the numbers of the array it receives more neatly.System.out.print.public static void printArrayInStars(int[] array) in the class named Main to make it print a row of stars for each number in the array.new, we can also initialize an array with a block, that contains comma-separated values to be assigned in the array.String[] arrayOfStrings = {"Matti L.", "Matti P.", "Matti V."};
double[] arrayOfFloatingpoints = {1.20, 3.14, 100.0, 0.6666666667};
for (int i = 0; i < arrayOfStrings.length; i++) {
System.out.println(arrayOfStrings[i] + " " + arrayOfFloatingpoints[i]);
}Sample Output
// index 0 1 2 3 4 5 6 7
int[] numbers = {100, 1, 42, 23, 1, 1, 3200, 3201};
// prints the number at index 0, i.e. number 100
System.out.println(numbers[0]);
// prints the number at index 2, i.e. number 42
System.out.println(numbers[2]);ArrayLists, you can’t access an index outside of the array.
Computer Language