Documentation Index
Fetch the complete documentation index at: https://v1-learn.neoartd.my.id/llms.txt
Use this file to discover all available pages before exploring further.
- An array is a sequence of values
- Used to store multiple values of the same type using a single variable
- The size of the array is fixed, determined when created
- The values in the array are called elements
- Elements in arrays are accessed using an index
- The index starts at 0, not 1
Creating Arrays
int[] data = new int[10];
double[] priceList;
priceList = new double[5];
String[] names = {"Ani", "Budi", "Cinta"};
Accessing Elements
int[] counts = new int[4];
counts[0] = 7;
counts[1] = counts[0] * 2;
counts[2]++;
counts[3] = counts[3] - 60;
Displaying Arrays
int[] values = {89, 50, 77};
System.out.println(values); // [I@15db9742
int[] values = {89, 50, 77};
System.out.println(values[0]);
System.out.println(values[1]);
System.out.println(values[2]);
int[] values = {89, 50, 77};
for (int i = 0; i < 3; i++) {
System.out.println(values[i]);
}
Array length
.length : digunakan untuk mengetahui panjang array yang dimiliki oleh suatu array
int[] values = {89, 50, 77};
for (int i = 0; i < 3; i++) {
System.out.println(values[i]);
}
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]);
}
Copying Arrays
int[] a = {89, 50, 77};
int[] b = a;
b[0] = b[0] - 20;
System.out.println(a[0]);
System.out.println(b[0]);
int[] a = {89, 50, 77};
int[] b = new int[a.length];
for (int i = 0; i < a.length; i++) {
b[i] = a[i];
}
int[] a = {89, 50, 77};
int[] b = Arrays.copyOf(a, a.length);
int[] c = Arrays.copyOf(a, 2);
Array Traversal
int[] values = {89, 50, 77};
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]);
}
for (int i = 0; i < values.length; i++) {
values[i] = values[i] * 2;
}
int[] values = {89, 50, 77};
for (int v : values) {
System.out.println(v);
}
String ⇔ char Array
String word = "lemon";
char[] characters = word.toCharArray();
characters[0] = 'm';
characters[2] = 'l';
String word2 = String.valueOf(characters);
System.out.println(word2);
Exercise
- Create an array that can hold 15 integers
- Fill up each element of the array with the number 123
- Then display the contents of the array

int[] numbers = new int[15];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = 123;
}
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element-" + i + " = " + numbers[i]);
}
- Create an array that can hold 5 integers
- Fill up the array with user inputs
- Then display the contents of the array

Scanner scanner = new Scanner(System.in);
int[] numbers = new int[5];
for (int i = 0; i < numbers.length; i++) {
System.out.print("Enter the value of Element-" + i + ": ");
numbers[i] = scanner.nextInt();
}
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element-" + i + " = " + numbers[i]);
}
- Create an array that can hold n integers
- n is user input
- Fill up the array with user inputs
- Then display the contents of the array

Scanner scanner = new Scanner(System.in);
System.out.print("Enter the Length of array: ");
int n = scanner.nextInt();
int[] numbers = new int[n];
for (int i = 0; i < numbers.length; i++) {
System.out.print("Enter the value of Element-" + i + ": ");
numbers[i] = scanner.nextInt();
}
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element-" + i + " = " + numbers[i]);
}
- Create an array that can hold n integers
- n is user input
- Fill up the array with user inputs
- Then display the contents of the array starting from the last element to the first

Scanner scanner = new Scanner(System.in);
System.out.print("Enter the Length of array: ");
int n = scanner.nextInt();
int[] numbers = new int[n];
for (int i = 0; i < numbers.length; i++) {
System.out.print("Enter the value of Element-" + i + ": ");
numbers[i] = scanner.nextInt();
}
for (int i = numbers.length - 1; i >= 0; i--) {
System.out.println("Element-" + i + " = " + numbers[i]);
}
- Create an array that can hold n integers
- n is user input
- Fill up the array with user inputs
- Then display the contents of the array starting from the last element to the first

Scanner scanner = new Scanner(System.in);
System.out.print("How many values: ");
int n = scanner.nextInt();
int[] numbers = new int[n];
for (int i = 0; i < numbers.length; i++) {
System.out.print("Enter the value-" + i + ": ");
numbers[i] = scanner.nextInt();
}
double sum = 0;
for (int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
double mean = sum / numbers.length;
double sumOfSquares = 0;
for (int i = 0; i < numbers.length; i++) {
sumOfSquares += Math.pow(numbers[i] - mean, 2);
}
double standardDeviation = Math.sqrt(sumOfSquares / numbers.length);
System.out.println("Standard Deviation = " + standardDeviation);
- Make a program to check whether a word entered by a user is palindrome or not
- A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward
- Examples:
- eye
- level
- radar
- madam
- refer
- …

Scanner scanner = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = scanner.next();
char[] characters = word.toCharArray();
boolean isPalindrome = true;
for (int i = 0; i < characters.length / 2; i++) {
if (characters[i] != characters[characters.length - 1 - i]) {
isPalindrome = false;
break;
}
}
if (isPalindrome) {
System.out.println("Palindrome");
} else {
System.out.println("Not a palindrome");
}
- Write a program that asks the user to enter the student’s name and grade n times.
- After all data is entered, the user can search for the grade of a student by entering his name.
- The program stops when the user enters “-” when asked for the name

Scanner scanner = new Scanner(System.in);
System.out.print("How many students? ");
int n = scanner.nextInt();
String[] names = new String[n];
int[] grades = new int[n];
for (int i = 0; i < n; i++) {
System.out.print("Name of student-" + i + ": ");
names[i] = scanner.next();
System.out.print("Grade of student-" + i + ": ");
grades[i] = scanner.nextInt();
}
while (true) {
System.out.print("Searching the grade of: ");
String name = scanner.next();
if (name.equals("-")) {
break;
}
boolean found = false;
for (int i = 0; i < n; i++) {
if (names[i].equals(name)) {
System.out.println(name + "\'s grade is " + grades[i]);
found = true;
break;
}
}
if (!found) {
System.out.println("Student not found");
}
}