Java---Chapter 4
an array
basic knowledge
concept:
An array is a common data structure in programming languages that can store a set of data of the same type
Function:
Store a set of data of the same type, convenient for mathematical statistics (maximum value, minimum value, average value and sum), and display information
definition:
public class study {
public static void main(String[] args) {
//1
byte[] bytes = new byte[]{
1,2,3,4,5};
//2
int[] numbers = {
1,2,3,4,5};
}
}
The first:
- Can only be used when defining an array and assigning a value at the same time
The second type:
- It can be used directly when defining an array, or it can be defined first and then assigned to use
Default values in array:
- The default value in an array of double-precision floating-point numbers is 0.0
- The default value in an array of single-precision floating-point numbers is 0.0f
- The default element of boolean type array is false
- The default element in the char type array is '\u0000'
- The default value for integer arrays is 0
Basic elements:
array operation
Array traversal:
look at all the elements in the array
The length of the array -----> array.length
public class study {
public static void main(String[] args) {
int[] arr1 = {
1,2,3,4,5};
for(int i=0;i<arr1.length;i++){
System.out.println(arr1[i]);
}
}
}
Array modification operation:
case:
the existing array 10, 12, 17, 32, 39, 50, it is required to square all the elements in the array that can be divisible by 3, and then put the element back to its position
public class study {
public static void main(String[] args) {
int[] arr1 = {
10,12,17,32,39,50};
for(int i=0;i<arr1.length;i++){
if(arr1[i]%3==0){
arr1[i] *= arr1[i];
}
}
}
}
Array addition operation
case:
At a certain air ticket agency, there are 4 people, A, B, C, D, E, who are queuing up to buy tickets. B’s good friend F is also queuing up to buy tickets now, and finds that B is queuing, so he jumps in line to B After that, please use the relevant knowledge of arrays to complete the program design
public class study {
public static void main(String[] args) {
String[] arr = {
"A","B","C","D","E"};
//A B C D E
//A B F C D E
String[] new_arr = new String[arr.length+1];
int co = 2;
for(int i=0;i<co;i++){
new_arr[i]=arr[i];
}
new_arr[co]="F";
for(int j=co;j<new_arr.length;j++){
new_arr[j+1]=arr[j];
}
arr = new_arr;
for(int m=0;m<arr.length;m++){
System.out.println(arr[m]);
}
}
}
Array delete operation
case 1:
In the previous case, the ticket buyer C left because of something, and there was one less person in the queue. Please use the relevant knowledge of the array to complete the program design
public class study {
public static void main(String[] args) {
String[] arr = {
"A","B","C","D","E"};
//A B C D E
//A B D E
String[] new_arr = new String[arr.length-1];
for(int i=0;i<2;i++){
new_arr[i]=arr[i];
}
for(int j=3;j<arr.length;j++){
new_arr[j-1]=arr[j];
}
arr = new_arr;
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
}
copy of the array
grammar:
System.arraycopy(原数组,拷贝的开始位置,目标数组,存放的开始位置,拷贝的元素个数);
In this way, we can replace the for loop copy in the above question with this syntax operation
String[] arr = {
"A","B","C","D","E"};
//A B C D E
//A B D E
String[] new_arr = new String[arr.length-1];
for(int i=0;i<2;i++){
new_arr[i]=arr[i];
}
The for loop is replaced by
System.arraycopy(arr,0,new_arr,0,2);
Array expansion
grammar:
数据类型[] 标识符 = Arrays.copyof(原数组,新数组的长度);
Example:
import java.util.Arrays;
public class study {
public static void main(String[] args) {
String[] arr = {
"A","B","C","D","E"};
//A B C D E
//A B C D E F
String[] new_arr = Arrays.copyOf(arr,arr.length+1);
}
}
Two array practice
array sort
The elements in the array are arranged in order from small to large, or from large to small. Divided into ascending order and descending order
Bubble Sort:
- Every time the array is traversed, a maximum value (maximum value, minimum value) can be obtained from the elements of the array
- When traversing the array each time, compare the size of two adjacent elements in the array, and exchange positions according to the arrangement requirements
Case:
Arrange the sequence 10,70,55,80,25,60 in descending order
public class study {
public static void main(String[] args) {
int[] arr = {
10,70,55,80,25,60};
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr.length-i-1;j++){
if(arr[j]<arr[j+1]){
int temp = arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(int j=0;j<arr.length;j++){
System.out.println(arr[j]);
}
}
}
Sorting operations for tools:
Syntax:
Arrays.sort(数组名);//将数组中的元素进行升序排列
Arrays.toString(数组名)//将数组中的元素组装成一个字符串
Note:
Characters can be sorted, sorted according to the order of the dictionary (abcdefg...)
import java.util.Arrays;
public class study {
public static void main(String[] args) {
String[] names = {
"zhangsan","zhangsi","lisi","lisan",
"lisiabc","lisib"};
Arrays.sort(names);
System.out.println(Arrays.toString(names));
}
}
[lisan, lisi, lisiabc, lisib, zhangsan, zhangsi]
binary search
Also known as half search, as the name suggests, it will be divided into two intervals from the middle each time, and the middle element will be used to compare the size of the element to be searched to determine the interval where the target element is located, and the range will be reduced in turn to determine the element
Binary search only works on sorted arrays
Case:
Quickly find out the position of element 60 from the sequence 95, 93, 87, 86, 79, 72, 60, 53
public class study {
public static void main(String[] args) {
int[] numbers = {
95,93,87,86,79,72,60,53};
int target = 60;
int start =0;
int end = numbers.length -1;
while(start<end){
int mid = (start+end)/2;
if(numbers[mid]>target)
{
start = mid;
}else if(numbers[mid]<target){
end = mid;
}else{
System.out.println(mid);
break;
}
}
}
}
Two-dimensional array
An array is essentially one-dimensional, and a two-dimensional array refers to adding a one-dimensional array to a one-dimensional array. Three-dimensional array, four-dimensional array and so on.
Definition of a two-dimensional array:
数据类型[][] 数组名 = new 数据类型[数组的长度][数组的长度]
For example: define a two-dimensional array with a length of 5, each space can only store double arrays of any length,
and 2 can also be left blank, which means that you can store...arrays of any length
public class study {
public static void main(String[] args) {
double[][] as = new double[5][];
as[0] = new double[]{
12,66};
as[1] = new double[]{
17,91};
as[2] = new double[]{
15,84};
as[3] = new double[]{
14,67};
as[4] = new double[]{
19,86};
}
}
Case 1:
Record 5 pieces of music information (including name, singer, publication year and month) from the console, and store the information in an array
import java.util.Scanner;
public class study {
public static void main(String[] args) {
String[][] music_a = new String[5][3];
Scanner sc = new Scanner(System.in);
for (int i=0;i<music_a.length;i++){
System.out.println("请输入名称:");
String name = sc.next();
System.out.println("请输入歌手:");
String singer = sc.next();
System.out.println("请输入出版年月:");
String date = sc.next();
music_a[i] = new String[]{
name,singer,date};
}
}
}
Case 2:
There are 3 classes in the first grade of a certain school, with 10 students in the first class, 8 students in the second class, and 7 students in the third class. Now it is required to input the grades and ages of the students in these 3 classes from the console. And calculate the average grade and average age of each class.
import java.util.Scanner;
public class study {
public static void main(String[] args) {
double[][][] stu_cal = new double[3][][];
stu_cal[0] = new double[10][2];
stu_cal[1] = new double[8][2];
stu_cal[2] = new double[7][2];
Scanner sc = new Scanner(System.in);
//录入数据
for(int i=0;i<stu_cal.length;i++){
double[][] ne_cal = stu_cal[i];
for(int j=0;j<stu_cal[i].length;j++){
System.out.println("请输入年龄:");
int age = sc.nextInt();
System.out.println("请输入成绩:");
double score = sc.nextDouble();
ne_cal[j] = new double[]{
age,score};
}
}
//查看数据,并计算平均值
for(int i=0;i<stu_cal.length;i++){
double tolage = 0,tolscore = 0;
double[][] ne_cal = stu_cal[i];
for(int j=0;j<ne_cal.length;j++){
tolage += ne_cal[j][0];
tolscore += ne_cal[j][1];
}
System.out.println("第"+(i+1)+"个班的平均年龄为"+(tolage/ne_cal.length));
System.out.println("第"+(i+1)+"个班的平均成绩为"+(tolscore/ne_cal.length));
}
}
}