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.
Multidimensional Arrays
int[][] a = new int[3][4];

int n = 5;
int[][] distance = new int[n][n];
distance[0][1] = distance[1][0] = 10;
distance[1][3] = distance[3][1] = 5;
int[][] a = {
{1, 2, 3},
{4, 5, 6, 9},
{7},
};

int[][] data = new int[4][];
data[0] = new int[3];
data[1] = new int[10];
data[2] = new int[2];
data[3] = new int[6];
Array Traversal
int[][] data = {
{ 1, 2, 3 },
{ 4, 5 },
{ 6, 7, 8, 9 }
};
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
System.out.print(data[i][j] + "\t");
}
System.out.println();
}
Exercise
Exercise 1
- Create an array that can hold n integers
- Fill up the array with user inputs
- Ask the user to enter a value that they want to find
- Show if the value is in the array

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the length of array: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.print("Enter the lookup value: ");
int lookup = sc.nextInt();
for (int i = 0; i < n; i++) {
if (arr[i] == lookup) {
System.out.println(lookup + " is in the array.");
}
}
}
}
Exercise 2
- Create an array that can hold n integers
- Fill up the array with user inputs
- Ask the user to enter a value that they want to find
- Show how many times the value appears in the array

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the length of array: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.print("Enter the lookup value: ");
int lookup = sc.nextInt();
int count = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == lookup) {
count++;
}
}
System.out.println(lookup + " appears " + count + " time(s) in the array.");
}
}
Exercise 3
- Create an array that can hold n integers
- Fill up the array with user inputs
- Ask the user to enter a value that they want to find
- Show whether the value is in the array or not

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the length of array: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.print("Enter the lookup value: ");
int lookup = sc.nextInt();
boolean found = false;
for (int i = 0; i < n; i++) {
if (arr[i] == lookup) {
found = true;
break;
}
}
if (found) {
System.out.println(lookup + " is in the array.");
} else {
System.out.println(lookup + " is not in the array.");
}
}
}
Exercise 4
- Create an array that can hold n integers
- Fill up the array with user inputs
- Ask the user to enter a value that they want to find
- Show on which index the value appears in the array

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the length of array: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.print("Enter the lookup value: ");
int lookup = sc.nextInt();
boolean found = false;
for (int i = 0; i < n; i++) {
if (arr[i] == lookup) {
found = true;
System.out.println(lookup + " is at index " + i + ".");
}
}
if (!found) {
System.out.println(lookup + " is not in the array.");
}
}
}
Exercise 5
- Create an array that can hold n integers
- Fill up the array with user inputs
- Find the largest value in the array
- Also display on which index the maximum value appears in the array

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the length of array: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int max = arr[0];
int index = 0;
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
index = i;
}
}
System.out.printf("Maximum value: %d", max);
System.out.printf("\nIndex: %d", index);
}
}
Exercise 6
- Create a 2-dimensional array that can hold m x n integers
- Fill up the array with user inputs
- Display the contents of the array

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("m: ");
int m = sc.nextInt();
System.out.print("n: ");
int n = sc.nextInt();
int[][] arr = new int[m][n];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = sc.nextInt();
}
}
System.out.println("Contents of the array:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
}
Exercise 7
- Create a 2-dimensional array that can hold m x n integers
- Fill up the array with user inputs
- Ask the user to enter a value that they want to find
- Show how many times the value appears in the array

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("m: ");
int m = sc.nextInt();
System.out.print("n: ");
int n = sc.nextInt();
int[][] arr = new int[m][n];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = sc.nextInt();
}
}
System.out.print("Enter the lookup value: ");
int lookup = sc.nextInt();
int count = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (arr[i][j] == lookup) {
count++;
}
}
}
System.out.println(lookup + " appears " + count + " time(s) in the array.");
}
}
Exercise 8
- Create a 2-dimensional array that can hold m x n integers
- Fill up the array with user inputs
- Find the largest value in the array
- Also display on which index the maximum value appears in the array

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("m: ");
int m = sc.nextInt();
System.out.print("n: ");
int n = sc.nextInt();
int[][] arr = new int[m][n];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = sc.nextInt();
}
}
int max = arr[0][0];
int maxI = 0;
int maxJ = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (arr[i][j] > max) {
max = arr[i][j];
maxI = i;
maxJ = j;
}
}
}
System.out.printf("Maximum value: %d\n", max);
System.out.printf("Index: (%d, %d)\n", maxI, maxJ);
}
}