首页 > 编程语言 > 详细

Java6 数组

时间:2021-05-31 12:19:23      阅读:20      评论:0      收藏:0      [点我收藏+]

数据类型[] 数组名称 = new 数据类型[长度];

 for(int temp:array){

  System.out.print(temp);

}

 

class ArrayDemo{
    public int  array[][] = new int[][]{{1,2,3,4,5},{2,4,8},{520,222,336}};
}
public class ArrayDemoDriver{
    public static void main(String[] args) {
        ArrayDemo example = new ArrayDemo();
        for(int i=0; i<example.array.length; i++){
            for(int j=0; j<example.array[i].length;j++){
                System.out.print(example.array[i][j]+"、");
            }
            System.out.println();
        }
    }
}

程序运行结果:

1、2、3、4、52、4、8520、222、336、

 

问:二维数组的赋值是否还有其它形式? 

 

 

数组排序


class ArraySort {
    public static int[] arraySort(int[] a){
        for(int i=0; i<a.length; i++){
            for(int j=0; j<a.length-i-1; j++){
                if(a[j]>a[j+1]){
                    int temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
        return a;
    }
    public static void arrayPrint(int[] a){
        for(int temp:a){
            System.out.print(temp+"、");
        }
    }

}
public class ArraySortDriver{
    public static void main(String[] args) {
        int[] array = new int[]{1,2,3,4,6,99,425,4626,73,5743,452,5,7,};
        ArraySort.arrayPrint(ArraySort.arraySort(array));
    }
}

程序运行结果:

1、2、3、4、5、6、7、73、99、425、452、4626、5743、

 

Java6 数组

原文:https://www.cnblogs.com/zhanlifeng/p/14829913.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!