Java basic practice questions Array (detailed)

For the use and definition of arrays, click the link below...
Definition and use of arrays

1. Print the contents of the array.

//用一个for循环进行遍历
public class demo01ArrayPrint {
    
    
    public static void main(String[] args) {
    
    
        int[] arr={
    
    1,2,3,4,5};
        for (int i = 0; i < arr.length; i++) {
    
    
        	//注意,此处不用换行,不然会不美观
            System.out.print(arr[i]+" ");
        }
        System.out.println();
    }
}

2. Create an integer array of length 6, randomly generate 6 integers between 0-100 and put them in the array,

        然后计算出数组中所有元素之和并且打印
public class demo02ArraySum {
    
    
    public static void main(String[] args) {
    
    
        Random r=new Random();
        //将sum的值初始化为0;
        int sum=0;
        int[] arr=new int[6];
        for (int i = 0; i < 6; i++) {
    
    
        	//用数组接收随机生成的整数,范围为[0,101),左闭右开.
            arr[i]=r.nextInt(101);
        }
        System.out.println("数组中的元素为:");
        
        for (int i = 0; i < arr.length; i++) {
    
    
        	//加一个" "是为了让遍历出来的数组值看起来美观
            System.out.print(arr[i]+" ");
            //sum=sum+arr[i];将数组中的值依次叠加起来.
            sum+=arr[i];
        }
        System.out.println();
        //输出最后的结果sum.
        System.out.print("数组中元素的和为:"+sum);
    }
}

3. Output the highest score among the students' grades.

public class demo03MaxScore {
    
    
    public static void main(String[] args) {
    
    
    	//初始化数组,将学生的成绩储存起来.
        int[] score={
    
    88,78,93,66,89,100};
        //将score[0]的值假设为最高的分数maxScore.
        int maxScore=score[0];
        //已经将score[0]的值设置为最大的,所以要从i=1开始循环
        for (int i = 1; i < score.length; i++) {
    
    
        	//用maxScore的值和数组中的元素进行比较,如果maxScore小于该元素
        	//则进行值的交换,这样会始终保持最大的值为maxScore
        	//最后输出maxScore.
            if (maxScore<score[i]){
    
    
                int temp=maxScore;
                maxScore=score[i];
                score[i]=temp;
            }
        }
        System.out.println("最高的成绩为:"+maxScore);
    }
}

4. Adjust the order of numbers in the array, put odd numbers together and even numbers together (regardless of the order).

public class demo04AdjustOrder {
    
    
    public static void main(String[] args) {
    
    
    	//初始化数组的值,你也可以修改代码,自己去给数组以以赋值
        int[] arr = {
    
    1, 2, 3, 4, 5, 6, 7, 8, 9,};
        for (int i = 0; i < arr.length; i++) {
    
    
            for (int j = 0; j < arr.length - 1 - i; j++) {
    
    
            	//如果是偶数,则和后一个元素互换位置
            	//那么数组就会变成前面是奇数,后面是偶数.
                if (arr[j] % 2 == 0) {
    
    
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                }
            }
        }
        //遍历打印数组.
        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.print(arr[i] + " ");
        }
    }
}

5. Create an array of int type with 100 elements, and set each element to 1 - 100 in turn

public class demo05BulidArray {
    
    
    public static void main(String[] args) {
    
    
    	//创建数组,元素个数为100.
        int[] arr=new int[100];
        /*
            也可以改为:
            	for (int i = 1; i < arr.length+1; i++) {
            		arr[i]=i;
            	}
            	效果一样.
        */
        for (int i = 0; i < arr.length; i++) {
    
    
        	//本来是0,1,2,~~~,99,i+1后为1,2,~~~,100.
            arr[i]=i+1;
        }
        int count=1;
        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.printf("%-2d ",arr[i]);
            //将数组内容每20个一行进行输出.
            if (count%20==0){
    
    
                System.out.println();
            }
            count++;
        }
        System.out.println();
    }
}

6. Taking the array as a parameter, the loop multiplies each element in the array by 2 and sets it to the corresponding array element. For example, the original array is {1, 2, 3}, and the modified one is {2, 4, 6}

public class demo06ChangeArrayNum {
    
    
    public static void main(String[] args) {
    
    
    	//初始化数组,你也可以改为自己输入.
        int[] arr={
    
    1,2,3,4,5,6};
        System.out.println("原数组是:");
        //循环遍历打印出初始数组,和改变后的数组形成一个对比
        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.print(arr[i]+" ");
        }
        System.out.println();
        //让数组每个元素都乘以2.
        for (int i = 0; i < arr.length; i++) {
    
    
            arr[i]*=2;
        }
        System.out.println("改变后的数组是:");
        //遍历打印出改变后的数组.
        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.print(arr[i]+" ");
        }
        System.out.println();
    }
}

7. Given an array of integers, arrange the elements in reverse order.

public class demo07ReverseArray {
    
    
    public static void main(String[] args) {
    
    
        //自己给定的数组,可随意更改.
        int[] arr={
    
    0,1,2,3,4,5,6,7,8,9};
        //设定两个下标, 分别指向第一个元素和最后一个元素.
        int left=0;
        int right=arr.length-1;
        /*
            判断,当左边的下标<右边的下标时,交换两个位置的元素.
                第一个和最后一个互换,第二个和倒数第二个互换,依此类推.
            当左边下标>=右边时,说明已经全部互换完成,实现数组的逆序
                不进入while循环.
         */
        while(left<right){
    
    
            int temp=arr[left];
            arr[left]=arr[right];
            arr[right]=temp;
            //左边下标依次递加,右边下标依次递减.
            left++;
            right--;
        }
        //for循环遍历输出互换后的数组.
        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.print(arr[i]+" ");
        }
        System.out.println();
    }
}

8.500 people encircle the city in a circle, start counting from 1, and every person who counts to a multiple of 3 leaves the circle, and the cycle repeats until there is only one person left in the last circle, and find the original position of the remaining people in the circle (Joseph circle)

package JavaArray;

public class demo08Joseph {
    
    
        public static void main(String[] args) {
    
    
            //声明长度为500数组,也可以在这里改变数组中的人数,即改变圈中人数
            boolean[] b = new boolean[500];
            //将数组中的元素全部设置为true,表示初始状态人都在圈中
            for(int i = 0;i<b.length;i++){
    
    
                b[i] = true;
            }
            //计数器,统计当前报数的位置
            int count = 0;
            //初始化圈中总人数
            int len = b.length;
            //初始化数组的索引
            int index = 0;
            //开始循环报数
            while(len > 1){
    
    
                //判断当前索引处的人是否在圈中
                if(b[index]){
    
    
                    //报数
                    count++;
                    //判断是否到达3的倍数
                    if(count == 3){
    
    
                        //计数器归零
                        count = 0;
                        //剩余人数减一
                        len--;
                        //将状态标记为离开(false)
                        b[index] = false;
                    }
                }
                //数组的索引递增
                index++;
                //如果数完一圈,则索引归零,这里很重要
                if(index == b.length){
    
    
                    index = 0;
                }
            }
            //遍历判断圈中剩余的最后一个为true的元素原来的索引即为剩下的人
            for(int i = 0;i<b.length;i++){
    
    
                if(b[i]){
    
    
                    System.out.println("剩余的人原来的位置:"+i);
                    break;
                }
            }
        }
}

Guess you like

Origin blog.csdn.net/weixin_47278183/article/details/120082877