【Array Exercise】


1. Find the most value

Define an array {33, 5, 22, 44, 55} and find the maximum value

Define an array to store 5 values

 int[] arr ={
    
    33, 5, 22, 44, 55};

Temporarily consider the data of index 0 to be the largest

int max = arr[0];

Compare each element with max

 for (int i = 1; i < arr.length; i++) {
    
    
           if(arr[i] > max)
           {
    
    
               max = arr[i];
           }

But after the loop ends, max records the maximum value

 System.out.println(max);

Total code block:

public class array_test {
    
    
    public static void main(String[] args) {
    
    
        int[] arr ={
    
    33, 5, 22, 44, 55};
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
    
    
           if(arr[i] > max)
           {
    
    
               max = arr[i];
           }
        }
        System.out.println(max);
    }
}

Second, sum and count the number

Traversing the array and
summing Requirements: Generate 10 random numbers between -100 and store them in the array.

  1. Find the sum of all data
  2. Find the average of all data
  3. Count how many data are smaller than the average

(1) Define an array

 int[] arr = new int[10];

(2) Generate random numbers

  Random r = new Random();

(3) Store the random number in the array


        for (int i = 0; i < arr.length; i++) {
    
    
            int number = r.nextInt(100) + 1;
            arr[i] = number;
        }
  1. Find the sum of all data
    Define the summation variable
 int sum = 0;

Loop to get each element and add it to sum

 for (int i = 0; i < arr.length; i++) {
    
    
            sum = sum + arr[i];
        }

Find the average of all data

int avg = sum / arr.length;
        System.out.println("平均数是:" + avg);

Count how many data are smaller than the average

 int count = 0;
        for (int i = 0; i < arr.length; i++) {
    
    
            if(arr[i] < avg){
    
    
                count++;
            }
        }
        System.out.println("随机数中比平均数小的有" + count + "个");

Data can be printed out for verification

 for (int i = 0; i < arr.length; i++) {
    
    
            System.out.print(arr[i] + " ");
        }

Total code block:

import java.util.Random;

public class array_test{
    
    
    public static void main(String[] args) {
    
    
        int[] arr = new int[10];
        Random r = new Random();

        for (int i = 0; i < arr.length; i++) {
    
    
            int number = r.nextInt(100) + 1;
            arr[i] = number;
        }
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
    
    
            sum = sum + arr[i];
        }
        System.out.println("数组和是:" + sum);

        int avg = sum / arr.length;
        System.out.println("平均数是:" + avg);

        int count = 0;
        for (int i = 0; i < arr.length; i++) {
    
    
            if(arr[i] < avg){
    
    
                count++;
            }
        }
        System.out.println("随机数中比平均数小的有" + count + "个");

        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.print(arr[i] + " ");
        }
    }
}

3. Exchange data

Requirements: Define an array, store 1, 2, 3, 4, 5, and exchange the elements corresponding to the index as required

  • Before swap: 1, 2, 3, 4, 5
  • After swap: 5, 4, 3, 2, 1

(1) Define an array to store data

 int[] arr ={
    
    1, 2, 3, 4, 5};

(2) Use loop to exchange data

 for(int i = 0,j = arr.length-1;i < j;i++,j--){
    
    
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }

(3) Print out data

  for (int i = 0; i < arr.length; i++) {
    
    
            System.out.print(arr[i] + " ");
        }

Total code block:

public class array_test {
    
    
    public static void main(String[] args) {
    
    
        int[] arr ={
    
    1, 2, 3, 4, 5};
        for(int i = 0,j = arr.length-1;i < j;i++,j--){
    
    
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.print(arr[i] + " ");
        }
    }
}

4. Shuffle the data

Requirements: Define an array, store 1-5. It is required to shuffle the order of all data in the array (random shuffle)

Define array storage 1-5

int[] arr = {
    
    1, 2, 3, 4, 5};

Loop through the array, shuffling the order of the data starting from index 0

 Random r= new Random();
        for (int i = 0; i < arr.length; i++) {
    
    
            int randomIndex = r.nextInt(arr.length);
            int temp = arr[i];
            arr[i] = arr[randomIndex];
            arr[randomIndex] = temp;
        }

Output the data in shuffled order

 for (int i = 0; i < arr.length; i++) {
    
    
            System.out.print(arr[i] + " ");
        }

Total code block:

import java.util.Random;

public class array_test {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = {
    
    1, 2, 3, 4, 5};
        Random r= new Random();
        for (int i = 0; i < arr.length; i++) {
    
    
            int randomIndex = r.nextInt(arr.length);
            int temp = arr[i];
            arr[i] = arr[randomIndex];
            arr[randomIndex] = temp;
        }
        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.print(arr[i] + " ");
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_64451048/article/details/127719970