javaSE 数组概述

数组概述

1.一维数组的创建及使用

(1)创建一维数组

1.先声明,再用new运算符进行内存分配
有两种方式:

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

声明数组后,还不能立即访问它的任何元素,因为数组只是给出了数组名字和元素的数据类型,要想真正使用数组,还要为它分配内存空间。在为数组分配内存空间时必须指明数组的长度。

数组名字=new 数组元素的类型[数组元素的个数]

使用new关键字为数组分配内存时,整型数组中各个元素的初始值都为0

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

数组元素的类型 数组名 = new 数组元素的类型[数组元素的个数]
int arr[]=new int[5];
(2)初始化一维数组

数组的初始化有以下两种形式

int arr[]=new int[]{1,2,3,4,5};   //第一种方法
int arr2[]={1,2,3,4,5};   //第二种方法

2.二维数组的创建及使用

(1) 二维数组的创建

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

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

对于高维数组,有两种为数组分配内存的方式
·直接分配内存

a=new int[2][3]

·分别为每一维分配内存

a=new int[2][];
a[0]=new int[2];
a[1]=new int[3];

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

int a[][]=new int[2][3]
int a[][]=new int[2][];
a[0]=new int[2];
a[1]=new int[3];
(2).二维数组初始化
数据类型 数组名称[][]={ 值1,值2……,值n } ;
int a[][]={{12,0},{45,10}};

3.数组的基本操作

(1)遍历数组

通常遍历数组使用for循环
遍历二维数组通常使用两层for循环,通过数组的length属性可获得数组长度

public class Demo {
    public static void main(String[] args) {
        int b[][]=new int[][]{{1},{2,3},{4,5,6}};
        for(int i=0;i<b.length;i++){
             for(int j=0;j<b[i].length;j++){
                 System.out.print(b[i][j]+" ");
             }
                System.out.println();
        }
    }
}
输出结果:
1 
2 3 
4 5 6 

遍历数组时,使用foreach语句可能会更简单

public class Demo {
    public static void main(String[] args) {
        int a[]=new int[]{1,2,3,4,5};
        for (int b:a)  {
            System.out.print(b+" ");
        }
    }
}
输出结果:
1 2 3 4 5 
public class Demo {
    public static void main(String[] args) {
        int b[][]=new int[][]{{1},{2,3},{4,5,6}};
        for(int x[]:b){
             for(int e:x){
                 System.out.print(e+" ");
             }
            System.out.println();
        }
    }
}

(2)填充替换数组元素

数组中的元素定义完成后,可通过Arrarys类的静态方法fill()来对数组中的元素进行替换.
fill()方法有两种参数类型
1.fill(int a[],int val)

Arrays.fill(int a[],int val)

a:要替换的数组
val:要替换的值

2.fill(int a[],int startindex,int endindex,int val)

Arrays.fill(int a[],int startindex,int endindex,int val)

a:要替换的数组
int startindex:要填充的第一个元素的位置(包括)
int endindex:要填充的最后一个元素的位置(不包括)
val:要替换的值

(3)对数组进行排序
Arrays.sort(object)
package com.code;

import java.util.Arrays;

//psvm
//sout
public class test {
    public static void main(String[] args)
    {
        int  arr[]=new int[]{23,42,12,8};
        Arrays.sort(arr);
        for(int x:arr){
            System.out.print(x+" ");
        }
        System.out.println();
    }
}
(4)复制数组

Arrays类的copyOf()方法与copyOfRange()方法可以实现对数组的复制。
1.copyOf()

copyOf(arr,int newlength)

copyOf()方法是复制数组至指定长度
arr:要进行复制的数组
newlength:复制后新数组的长度。如果新数组的长度大于数组arr的长度,则用0填充(根据复制数组的类型来决定填充的值,整型数组用0填充,char型数组则用null)

public class test {
    public static void main(String[] args)
    {
        int  arr[]=new int[]{23,42,12,8};
        int newarr[]=Arrays.copyOf(arr,2);
        for(int x:newarr){
            System.out.print(x+" ");
        }
        System.out.println();
    }
}

输出结果:
23 42 

2.copyOfRange()

copOfRange(ar,int formindex,int toindex)

arr:要复制的数组对象
fromindex:指定开始复制数组的索引位置。fromindex必须在0至整个数组的长度之间。新数组包括fromindex位置的元素
toindex:要复制范围的最后索引位置。可大于arr的长度。新数组不包括toindex位置的元素

public class test {
    public static void main(String[] args)
    {
        int  arr[]=new int[]{23,42,12,8,9,10};
        int newarr[]=Arrays.copyOfRange(arr,0,3);
        for(int x:newarr){
            System.out.print(x+" ");
        }
        System.out.println();
    }
}

输出结果:
23 42 12   
(5)数组查询

Arrays类的binarySearch()方法,可使用二分搜索法来搜索指定数组,以获得指定对象。该方法返回要搜索元素的索引值。
binarySearch()方法有两种参数类型
1.binarySearch(Object[],Object key)

Arrays.binarySearch(Object[] a,Object key)

a:要搜素的数组
key:要搜索的值

搜索之前必须排序,如果不排序结果是不确定的
如果数组有多个带有指定值的元素,则结果也不确定

public class test {
    public static void main(String[] args)
    {
        int  arr[]=new int[]{23,42,12,8,9,10};
        Arrays.sort(arr);   //搜索之前必须排序
        int index=Arrays.binarySearch(arr,8);
        System.out.println("8的索引位置是:"+index);
    }
}
输出结果:
8的索引位置是:0

2.binarySearch(Object[],int fromindex,int toindex,Object key)

Arrays.binarySearch(Object[],int fromindex,int toindex,Object key)

a:要搜素的数组
fromindex:指定范围的开始处索引(包括)
toindex:指定范围的结束索引(不包括)
key:要搜索的值

如果搜索的元素在指定范围内,则返回搜索值得索引。否则返回-1或“-”(插入点)/如果范围中的所有元素都小于指定元素的值,则为toindex

public class test {
    public static void main(String[] args)
    {
        int  arr[]=new int[]{23,42,12,8,9,10};
        Arrays.sort(arr);
        int index=Arrays.binarySearch(arr,0,2,8);
        int index2=Arrays.binarySearch(arr,3,5,9);
        System.out.println("8的索引位置是:"+index);
        System.out.println("9的索引位置是:"+index2);
    }
}
输出结果:
8的索引位置是:0
9的索引位置是:-4
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值