# Beginner log 12 / 15 array

Posted by thessoro on Wed, 20 May 2020 17:14:23 +0200

```It took a week to learn about variables, data types and operators, selection structure, circular structure and circular nesting. Today, I entered into the study of array. The content is not much, and the focus is on practice.
Here are four exercises you did today:
```
```package videoCourseOfJava;

import java.util.Scanner;

/**
* @author A fish's Foam
* @date: 2017/12/15
* @see Enter an array from the console and operate on it
*/
public class Array 01 {

// private static int[] score;

public static void main(String[] args) {
int[] score = new int;//Declare an array and allocate memory space for the array
Scanner sc = new Scanner(System.in);// Get information from the console using the Scanner method (here is the data information used to get the array)
for (int i = 0; i < score.length; i++) {// score.length Represents the length of the fraction array. You can also enter a specific value
System.out.print("Please enter the" + (i + 1) + "Scores of students:");
score[i] = sc.nextInt();// Get an array with a length of 7 from the console
}
int sum = 0;// Define a total score with an initial value of 0
for (int i = 0; i < score.length; i++) {//Using the for loop structure to calculate the sum of the score s entered from the console
sum = sum + score[i];// Change the initial value of the total score and sum it up
}
double avg = sum / 5.0;// Declare and assign to find the average
System.out.println("The total score entered is:" + sum);
System.out.println("The average score entered is:" + avg);
sc.close();// Close input stream, free memory
}
}
``` ```package videoCourseOfJava;

import java.util.Scanner;

/**
* @author A fish's Foam
* @see Sum of array and verification of data in array
*/
public class Array 02 {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);// Call Scanner method
int[] nums = { 84, 75, 39, 68, 28, 96, 76 };// Declare arrays and assign values collectively
int sum = 0;// Defines the initial value of an integer type, which is used to sum integers
System.out.print("The array is:");
for (int i = 0; i < nums.length; i++) {// nums.length Represents the length of the nums array
System.out.print(nums[i] + "  ");
sum += nums[i];// Sum up the sum of all the values in the nums array
}
System.out.println("The total data of the array is:" + sum);
System.out.print("Please enter the number to find:");
int n = sc.nextInt();// Get an integer data from the console
for (int i = 0; i < nums.length; i++) {
if (n == nums[i]) {// In turn, verify whether the data n is equal to a certain value in the given array, and if so, execute the statement in the if structure
System.out.print("The number you are looking for is in the" + (i + 1) + "Number appears");
}
}
sc.close();// Close the input stream and free memory
}

}
``` ```package videoCourseOfJava;

import java.util.Arrays;//Array to sort the referenced packages
import java.util.Scanner;

/**
* @author A fish's Foam
* @see Console input array and arrange it in ascending order automatically
*/
public class Array 03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] score = new int;
for (int i = 0; i < score.length; i++) {
score[i] = sc.nextInt();
}
Arrays.sort(score);// Array sort command, used to sort the data in the array from small to large
System.out.print("The sorted array elements are:");
for (int i : score) {// foreach is mainly used to traverse an array or a collection, indicating that the values in score are assigned to i in turn
System.out.print(i + " ");
}
sc.close();// Close the input stream and free memory
}
}
``` ```package videoCourseOfJava;

import java.util.Scanner;

/**
* @author A fish's Foam
* @date 2017/12/15
* @see Insert algorithm: add new elements to a given array, which can be used to deal with problems such as supplementary student scores
*/
public class Array 04 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
// int[] list=new int[]{99,85,82,63,60}; / / when performing the insertion algorithm, data declaration should not be performed collectively
int[] list = new int;// Declare arrays and allocate space
list = 99;
list = 85;
list = 82;
list = 63;
list = 60;// Assign values to 5 of them
System.out.println("The array elements before insertion are:");
for (int val : list) {// Assign the values in the list to val in turn
System.out.println(val);// In the output array, the value of list is 0 by default
}
System.out.println("Please enter the value to insert:");
int num = sc.nextInt();// Enter a value to be inserted from the console
// Declare where variable records are to be inserted
int index = -1;// Index is the index of the number of inserts
for (int i = 0; i < list.length; i++) {
if (num > list[i]) {// In the array, exclude the numbers larger than num in the subscript order, find the first number smaller than the inserted value, and assign the subscript of this number to index
index = i;// Location to find
break;
}
}
for (int j = list.length - 1; j > index; j--) {
// Starting from the last number in the arrangement, that is list.length-1 start to assign the previous number to the next number in turn until the index position (excluding)
// The effect is to move the whole array after the assigned index backward one position
list[j] = list[j - 1];// Move the number of values corresponding to the subscript whose subscript value is larger than the index backward as a whole, that is, give way to the index
}
// Place the value to be inserted in the index
list[index] = num;// Assign the inserted value to the inserted position (index)
System.out.println("The array elements after insertion are:");
for (int val : list) {// Assign the values in the list to val in turn
System.out.println(val);// Output array for new sort
}
sc.close();// Close the input stream and free memory
}

}
``` [a little bit of gossip] from English to java programming, there was a little bit of thinking that couldn't be converted for a while. In fact, many mathematical and logical algorithms were not too difficult to understand, but they were still understood after repeated reading for several times. When I was in high school, I was a liberal arts major, but my mathematical thinking at that time was not bad, but I haven't been exposed to any mathematics since college. Now, in fact, I'm a little thankful, because now in my life, there is not only the perceptual thing like literature, but also a little rational constraint.
Next, I'll go to "classes and objects". I've seen this chapter before, but it seems very difficult. I hope I can get through it safely.

Topics: Java Programming