java数组(上)

数组:数组是具有相同数据类型的一组数据的集合。数组每个元素具有相同的数据类型。

在程序设计中引入数组可以更有效的管理和处理数据。

一维数组的创建及使用:

创建一维数组:

1、先声明,再用new运算符进行内存分配

数组元素类型 数组名字[]

int arr[];

int是数组元素类型 arr为名字[]

声明后不能立即访问它的任何元素,因为声明数组只是给出了数组名字和元素的数据类型,

想要真正的使用数组,还要为它进行分配内存空间。

int arr[]=new int[5];

分配内存空间为int类型分配的内存空间为5个元素。

2.声明的同时为数组分配内存

创建数组的方法是将数组的声明和内存的分配何在一起执行。

int month[]=new int [12];

上面的代码创建数组为month,并指定了长度为12。

初始化一维数组:

数组与基本数据类型一样可以进行初始化操作。数组的初始化可分别初始化数组中的每个元素。

比如:

int arr[]=new int{1,2,3,5,25};//为第一种形式

int arr2[]={34,23,12,16};//第二种形式

使用一维数组

在主方法中创建int型数组,并实现将个月的天数输出:

public class GetDay {

    public static void main(String[] args) {
        int day[]=new int[]{31,28,31,30,31,30,31,31,31,30,31,30,31};
        for(int i=0;i<12;i++){
            System.out.println((i+1)+"月有"+day[i]+"天");
        }
    }
}

执行结果:

1月有31天
2月有28天
3月有31天
4月有30天
5月有31天
6月有30天
7月有31天
8月有31天
9月有31天
10月有30天
11月有31天
12月有30天

二维数组的创建及使用

二维数组的创建:

格式为:数组元素的类型数组名字[][];

              int myarr[][];

              数组元素的类型[][]数组名字;

             int a=new int[2][4]

使用二维数组的方法:

用二维数组输出一个3行4列且所有元素的0的矩阵

public class Matrix {
    public static void main(String[] args) {
        int a[][]=new int[3][4];//定义二维数组
        for(int i=0;i<a.length;i++){//循环遍历数组中的每个元素
            System.out.println(a[i][i]);
        }
        System.out.println();
    }
}

执行结果为:

0
0
0

遍历数组:

定义二维数组,实现将二维数组的元素呈梯形输出

使用for循环来实现

public class Trap {
   public static void main(String[] args) {
    int b[][]=new int[][]{{1},{2,3},{4,5,6}};
    for(int k=0;k<b.length;k++){
        for(int c=0;c<b[k].length;c++){
            System.out.print(b[k][c]);
        }
        System.out.println();
    }
}
}

执行结果为:

1
23
456

使用增强for(foreach)来实现二维数组的遍历:

public class Tautog {
    //使用foreach遍历二维数组
    public static void main(String[] args) {
        int arr2[][]={{4,3},{1,2}};
        System.out.println("数组元素是:");
        int i=0;
        for(int x[]:arr2){
            i++;
            int j=0;
            for(int e:x){
                j++;
                if(i==arr2.length && j==x.length){
                    System.out.println(e);
                }else{
                    System.out.print(e+"、");
                }
            }
        }
    }
}
4、3、1、2

填充替换数组元素

数组的元素定义完成后,可通过Arrays类的静态方法fill()来对数组中的元素进行替换。

对数组进行排序

通过Arrays类的静态sort方法可以实现对数组的排序。sort方法提供了多种重载形式,可对任意类型的数组

进行排序。

public class Taxis {

    public static void main(String[] args) {
        int arr[]=new int[]{23,42,12,8};
        Arrays.sort(arr);
        for(int i=0;i<arr.length;i++){
            System.out.println(arr[i]);
        }

    }

}

执行结果为:

8
12
23
42

复制数组:

//Array类的copyof()方法与copyOfRange()方法可以实现对数组进行复制。copy()
//方法是复制数据至指定长度,copyOfRange()方法则将指定数组的指定长度复制到
//一个新数组中
public class copy {
//实现数组长度复制长度为5的数组
    public static void main(String[] args) {
        int arr[]=new int[]{23,42,12};
        int newarr[]=Arrays.copyOf(arr, 5);
        for(int i=0;i<newarr.length;i++){
            System.out.println(newarr[i]);
        }
    }
}

执行结果为:

23
42
12
0
0

//将数组索引位置是0~3之间的元素复制到新数组中
public class Repeat {

    public static void main(String[] args) {
        int arr[]=new int[]{23,42,12,84,10};
        int newarr[]=Arrays.copyOfRange(arr,0,3);
        for(int i=0;i<newarr.length;i++){
            System.out.println(newarr[i]);
        }
    }

}

执行结果为:

23
42
12

数组查询:

Arrays类的binarySearch()方法,可使用二分搜索法来搜索指定数组,以获得指定对象。

该方法返回搜索元素的索引值。

public class Example {

    public static void main(String[] args) {
        int arr[]=new int[]{1,8,9,4,5};
        Arrays.sort(arr);
        int index=Arrays.binarySearch(arr, 4);
        System.out.println("4的索引位置是"+index);
    }

}

执行结果为:

4的索引位置是1

在使用之前同样要对数组进行排序,来获得准确的索引值。

如果要搜索的元素key在指定范围内,则返回搜索键的索引:

否则返回-1或“-”(插入点)。如果范围中的所有元素都小于指定键,

则为toIndex(注意,这保证了当且仅当此键被找到时,返回的值将大于等于0).

实现查找元素“cd”在指定范围的数组str的索引位置。

public class Rake {

    public static void main(String[] args) {
        String str[]=new String[]{"ab","cd","ef","yz"};
        Arrays.sort(str);
        //在指定的范围内搜索元素"cd"的索引位置
        int index=Arrays.binarySearch(str, 0,2,"cd");
        System.out.println("cd的索引位置是:"+index);
    }

}

执行结果为:

cd的索引位置是:1

原文地址:https://www.cnblogs.com/wzhdcyy/p/9282344.html