Java4数组

课后:

1、编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)[必做题]

int a[]={10,20,30,40,50};

for(int i=0;i<a.length;i++) {

System.out.println(a[i]);

}

2、将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。(知识点:数组复制) [必做题]

public static void main(String[] args) {

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

int b[]= {7,8,9,10,11};

int aa1[]=a.clone();

int bb1[]=b.clone();

System.out.println(Arrays.toString(aa1));

System.out.println(Arrays.toString(bb1));

}

}

3、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序) [必做题]

public static void main(String[] args) {

int temp;

int[] arr = new int[] { 1, 6, 2, 3, 9, 4, 5, 7, 8 };

Arrays.sort(arr);// java提供对数组排序

for (int n : arr) {

System.out.println(n);

}

}

4:输出一个double型二维数组(长度分别为5、4,值自己设定)的值。(知识点:数组定义和创建、多维数组初始化、数组遍历) [必做题]

public static void main(String[] args) {

       double a[][] = new double[5][4];

       // 为这个二维数组赋值

       for (int i = 0; i < 5; i++) {

//         第一层i循环

           for (int j = 0; j < 4; j++) {

//            第二层j循环

              a[i][j] = Integer.parseInt(i + "" + j);

              System.out.print(a[i][j] + " ");

           }

           System.out.println();

       }

      

    }

 

5、 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问) [必做题]

 

 public static void main(String[] args) {

      

       int[] arr = new int[] { 18, 25, 7, 36, 13, 2, 89, 63 };

       int max = arr[0];

       for (int i = 0; i < arr.length; i++) {

           if (max < arr[i]) {

              max = arr[i];

           }

           System.out.println(i);

       }

       System.out.println(max);

    }

原文地址:https://www.cnblogs.com/liuyanhuiyanhui/p/12405497.html