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

# Arrays 1

![Arrays](https://i.ibb.co/CVz5ZTS/Screenshot-2024-04-03-194447.png)

* 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

```java theme={null}
int[] data = new int[10];

double[] priceList;
priceList = new double[5];

String[] names = {"Ani", "Budi", "Cinta"};
```

## Accessing Elements

<Tabs>
  <Tab title="Code">
    ```java theme={null}
    int[] counts = new int[4];
    ```
  </Tab>

  <Tab title="Ilustrasi">
    ![1](https://i.ibb.co/jRZg8Dn/Screenshot-2024-04-03-194726.png)
  </Tab>
</Tabs>

<Tabs>
  <Tab title="Code">
    ```java theme={null}
    counts[0] = 7;
    counts[1] = counts[0] * 2;
    counts[2]++;
    counts[3] = counts[3] - 60;
    ```
  </Tab>

  <Tab title="Ilustrasi">
    ![2](https://i.ibb.co/ZmJjN1W/Screenshot-2024-04-03-194735.png)
  </Tab>
</Tabs>

## Displaying Arrays

<Tabs>
  <Tab title="False">
    ```java theme={null}
    int[] values = {89, 50, 77};
    System.out.println(values); // [I@15db9742
    ```
  </Tab>

  <Tab title="True">
    ```java theme={null}
    int[] values = {89, 50, 77};
    System.out.println(values[0]);
    System.out.println(values[1]);
    System.out.println(values[2]);
    ```
  </Tab>

  <Tab title="True">
    ```java theme={null}
    int[] values = {89, 50, 77};
    for (int i = 0; i < 3; i++) {
        System.out.println(values[i]);
    }
    ```
  </Tab>
</Tabs>

## Array length

`.length` : digunakan untuk mengetahui panjang array yang dimiliki oleh suatu array

```java theme={null}
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

<Tabs>
  <Tab title="False">
    ```java theme={null}
    int[] a = {89, 50, 77};
    int[] b = a;
    b[0] = b[0] - 20;
    System.out.println(a[0]);
    System.out.println(b[0]);
    ```
  </Tab>

  <Tab title="True">
    ```java theme={null}
    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);
    ```
  </Tab>
</Tabs>

## Array Traversal

<Tabs>
  <Tab title="True">
    ```java theme={null}
    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;
    }
    ```
  </Tab>

  <Tab title="True">
    ```java theme={null}
    int[] values = {89, 50, 77};
    for (int v : values) {
        System.out.println(v);
    }
    ```
  </Tab>
</Tabs>

## String $\Leftrightarrow$ char Array

```java theme={null}
String word = "lemon";
char[] characters = word.toCharArray();

characters[0] = 'm';
characters[2] = 'l';

String word2 = String.valueOf(characters);

System.out.println(word2);
```

## Exercise

<Tabs>
  <Tab title="Question 1">
    * 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
      ![Question 1](https://i.ibb.co/B4zgRnC/Screenshot-2024-04-03-200449.png)
  </Tab>

  <Tab title="Answer 1">
    ```java theme={null}
    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]);
    }
    ```
  </Tab>
</Tabs>

<Tabs>
  <Tab title="Question 2">
    * Create an array that can hold 5 integers
    * Fill up the array with user inputs
    * Then display the contents of the array
      ![Question 2](https://i.ibb.co/fnb86bX/Screenshot-2024-04-03-200456.png)
  </Tab>

  <Tab title="Answer 2">
    ```java theme={null}
    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]);
    }
    ```
  </Tab>
</Tabs>

<Tabs>
  <Tab title="Question 3">
    * 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
      ![Question 3](https://i.ibb.co/rKjtjjh/Screenshot-2024-04-03-200503.png)
  </Tab>

  <Tab title="Answer 3">
    ```java theme={null}
    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]);
    }
    ```
  </Tab>
</Tabs>

<Tabs>
  <Tab title="Question 4">
    * 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
      ![Question 4](https://i.ibb.co/tLXqy91/Screenshot-2024-04-03-200512.png)
  </Tab>

  <Tab title="Answer 4">
    ```java theme={null}
    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]);
    }
    ```
  </Tab>
</Tabs>

<Tabs>
  <Tab title="Question 5">
    * 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
      ![Question 5](https://i.ibb.co/sspZY5r/Screenshot-2024-04-03-200525.png)
      ![Question 5](https://i.ibb.co/YQY9sM0/Screenshot-2024-04-03-200518.png)
  </Tab>

  <Tab title="Answer 5">
    ```java theme={null}
    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);
    ```
  </Tab>
</Tabs>

<Tabs>
  <Tab title="Question 6">
    * 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
      * ...
        ![Question 6](https://i.ibb.co/hLK8Xb0/Screenshot-2024-04-03-200531.png)
  </Tab>

  <Tab title="Answer 6">
    ```java theme={null}
    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");
    }
    ```
  </Tab>
</Tabs>

<Tabs>
  <Tab title="Question 7">
    * 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
      ![Question 7](https://i.ibb.co/W542npv/Screenshot-2024-04-03-200544.png)
  </Tab>

  <Tab title="Answer 7">
    ```java theme={null}
    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");
        }
    }
    ```
  </Tab>
</Tabs>
