Java basics --- one-dimensional array

Java basics-one-dimensional array

Preface: Why do you need an
array? An array is a variable used to store data of the same data type.
Insert picture description here
Insert picture description here
Insert picture description here
【Note:Array index starts from 0], if you don’t pay attention, the following exception will appear:Array subscript out of bounds exception
Insert picture description here

Declaration and definition of one-dimensional arrays

  1. Declare the array: int[] a;
  2. Assign Kong Jian: a = new int[5];
  3. Assignment: a[0] = 8;
  4. Processing data: a[0]=a[0]*10;

Common ways to define one-dimensional arrays:
int[] a =new int[5];
a[0] =8,a[1]=34...
int[] a = {12,23,23};

【example】

1. Calculate the average score of 5 students
1. Useless for loop

int [ ] scores = {60, 80, 90, 70, 85};
double avg = (scores[0] + scores[1] + scores[2] + scores[3] + scores[4])/5;
System.out.println(avg);

2. Use a for loop

int [ ] scores = {60, 80, 90, 70, 85};
int sum = 0;
double avg;
for(int i = 0; i < scores.length; i++){
    sum = sum + scores[i];
}
System.out.println(avg = sum / scores.length);

The actual address of the data in memory
Insert picture description here
2. Calculate the sum of all elements in an array, and determine whether the input number is in the array

int []arr = {8,4,2,1,23,344,12};
int sum = 0;
for (int x:arr) {
    System.out.print(x+"\t");
    sum+=x;
}
System.out.println("\n总和为"+sum);	 		//输处数组中所有元素的总和
System.out.println("请输入一个数");         //输入一个数看是否为数组中元素
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
boolean tag =false;
for (int i = 0; i < arr.length; i++) {
    if (input == arr[i])
        tag = true;
}
if (tag) {
    System.out.println(tag);
} else {
    System.out.println(tag);
}

[Note: The output here is a bit basic, we will talk about the Arrays.toString(a) method to quickly print all elements of the array]

The results show:
Insert picture description here
Three, find the value of the largest element in an array

int a[] = new int[10];                   //求最大值
for (int i = 0; i < a.length; i++) {
    a[i] = (int)(Math.random()*100);	//这边就用随机数代替输入
}
int max = a[0];							//默认第一个元素为最大值,同理求最小值也是一样
for (int i : a) {
    System.out.print(i+"\t");
    if(max<=i)
        max = i;
}
System.out.println("\n"+max);

The results show:
Insert picture description here
Fourth, insert a number in the array

int[] a = {99,85,33,22,-43};             //数组插入元素,采用 从后往前比较
int[] b = Arrays.copyOf(a,6);
System.out.println(Arrays.toString(a));
int num = 100;							//要插入的数,你也可以改成自己输入
int x = 1;
for (int i = b.length-2; i >=0 ; i--) {
    if(b[i]<num){
        b[i+1] = b[i];        			//比num小的元素,往后移位
    }else {
        b[i+1] = num;        		   //比num大的,将num插入到该元素后面
        x = 2;
        break;
    }
}
if(x==1){                   		  //如果没有没有比num大的,将num插在数组第一位
    b[0] = num;
}
System.out.println(Arrays.toString(b));

The results show that:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43288259/article/details/112514260