JAVA基础:数组、增强型for循环、Arrays

来源:how2j
创建数组
数组是一个固定长度的,包含了相同类型数据的容器
声明数组 :
int[] a; 声明了一个数组变量。
[]表示该变量是一个数组
int 表示数组里的每一个元素都是一个整数
a 是变量名
但是,仅仅是这一句声明,不会创建数组
有时候也会写成int a[]; 没有任何区别

public class HelloWorld {
    public static void main(String[] args) {
        // 声明一个数组
        int[] a;
    }
}

创建数组:
创建数组的时候,要指明数组的长度。
new int[5]
引用概念:
如果变量代表一个数组,比如a,我们把a叫做引用
与基本类型不同
int c = 5; 这叫给c赋值为5
声明一个引用 int[] a;
a = new int[5];
让a这个引用,指向数组

public class HelloWorld {
    public static void main(String[] args) {
        //声明一个引用
        int[] a;
        //创建一个长度是5的数组,并且使用引用a指向该数组
        a = new int[5];
         
        int[] b = new int[5]; //声明的同时,指向一个数组
         
    }
}

访问数组 :

public class HelloWorld {
    public static void main(String[] args) {
        int[] a;
        a = new int[5];
         
        a[0]= 1;  //下标0,代表数组里的第一个数
        a[1]= 2;
        a[2]= 3;
        a[3]= 4;
        a[4]= 5;
    }
}

数组长度 :
.length属性用于访问一个数组的长度
数组访问下标范围是0到长度-1
一旦超过这个范围,就会产生数组下标越界异常

public class HelloWorld {
    public static void main(String[] args) {
        int[] a;
        a = new int[5];
         
        System.out.println(a.length); //打印数组的长度
         
        a[4]=100; //下标4,实质上是“第5个”,既最后一个
        a[5]=101; //下标5,实质上是“第6个”,超出范围 ,产生数组下标越界异常
         
    }
}

数组初始化
分配空间与赋值分步进行

public class HelloWorld {
    public static void main(String[] args) {
        int[] a = new int[5]; //分配了长度是5的数组,但是没有赋值
         
        //没有赋值,那么就会使用默认值
        //作为int类型的数组,默认值是0
        System.out.println(a[0]);
         
        //进行赋值
     
        a[0] = 100;
        a[1] = 101;
        a[2] = 103;
        a[3] = 120;
        a[4] = 140;
    }
}

分配空间,同时赋值

public class HelloWorld {
    public static void main(String[] args) {
        //写法一: 分配空间同时赋值
        int[] a = new int[]{100,102,444,836,3236};
 
        //写法二: 省略了new int[],效果一样
        int[] b = {100,102,444,836,3236};
         
        //写法三:同时分配空间,和指定内容
        //在这个例子里,长度是3,内容是5个,产生矛盾了
        //所以如果指定了数组的内容,就不能同时设置数组的长度
        //int[] c = new int[3]{100,102,444,836,3236};
         
    }
}

增强型for循环
增强型for循环只能用来取值,却不能用来修改数组里的值

public class HelloWorld {
    public static void main(String[] args) {
        int values [] = new int[]{18,62,68,82,65,9};
        //常规遍历
        for (int i = 0; i < values.length; i++) {
            int each = values[i];
            System.out.println(each);
        }
         
        //增强型for循环遍历
        for (int each : values) {
            System.out.println(each);
        }
         
    }
}

复制数组
System.arraycopy(src, srcPos, dest, destPos, length)
src: 源数组
srcPos: 从源数组复制数据的起始位置
dest: 目标数组
destPos: 复制到目标数组的起始位置
length: 复制的长度

public class HelloWorld {
    public static void main(String[] args) {
        int a [] = new int[]{18,62,68,82,65,9};
         
        int b[] = new int[3];//分配了长度是3的空间,但是没有赋值
         
        //通过数组赋值把,a数组的前3位赋值到b数组
         
        //方法一: for循环
         
        for (int i = 0; i < b.length; i++) {
            b[i] = a[i];
        }
        
        //方法二: System.arraycopy(src, srcPos, dest, destPos, length)
        //src: 源数组
        //srcPos: 从源数组复制数据的启始位置
        //dest: 目标数组
        //destPos: 复制到目标数组的启始位置
        //length: 复制的长度       
        System.arraycopy(a, 0, b, 0, 3);
         
        //把内容打印出来
        for (int i = 0; i < b.length; i++) {
            System.out.print(b[i] + " ");
        }
 
    }
}

二维数组
int b[][] = new int[][]{
{1,2,3},
{4,5,6},
{7,8,9}
};
初始化二维数组

public class HelloWorld {
    public static void main(String[] args) {
       //初始化二维数组,
       int[][] a = new int[2][3]; //有两个一维数组,每个一维数组的长度是3
       a[1][2] = 5;  //可以直接访问一维数组,因为已经分配了空间
          
       //只分配了二维数组
       int[][] b = new int[2][]; //有两个一维数组,每个一维数组的长度暂未分配
       b[0]  =new int[3]; //必须事先分配长度,才可以访问
       b[0][2] = 5;
        
       //指定内容的同时,分配空间
       int[][] c = new int[][]{
               {1,2,4},
               {4,5},
               {6,7,8,9}
       };
 
    }
}

Arrays
import java.util.Arrays;
Arrays是针对数组的工具类,可以进行 排序,查找,复制填充等功能。

copyOfRange 数组复制

import java.util.Arrays;
 
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
 
        // copyOfRange(int[] original, int from, int to)
        // 第一个参数表示源数组
        // 第二个参数表示开始位置(取得到)
        // 第三个参数表示结束位置(取不到)
        int[] b = Arrays.copyOfRange(a, 0, 3);
 
        for (int i = 0; i < b.length; i++) {
            System.out.print(b[i] + " ");
        }
 
    }
}

toString() 转换为字符串

import java.util.Arrays;
  
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
        String content = Arrays.toString(a);
        System.out.println(content);
        //[18, 62, 68, 82, 65, 9]
  
    }
}

sort 排序

import java.util.Arrays;
  
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
        System.out.println("排序之前 :");
        System.out.println(Arrays.toString(a));
        Arrays.sort(a);
        System.out.println("排序之后:");
        System.out.println(Arrays.toString(a));
  
    }
}

binarySearch 搜索
查询元素出现的位置
需要注意的是,使用binarySearch进行查找之前,必须使用sort进行排序
如果数组中有多个相同的元素,查找结果是不确定的

import java.util.Arrays;
 
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
 
        Arrays.sort(a);
 
        System.out.println(Arrays.toString(a));
        //使用binarySearch之前,必须先使用sort进行排序
        System.out.println("数字 62出现的位置:"+Arrays.binarySearch(a, 62));
    }
}

equals 判断是否相同
比较两个数组的内容是否一样
第二个数组的最后一个元素是8,和第一个数组不一样,所以比较结果是false

import java.util.Arrays;
 
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
        int b[] = new int[] { 18, 62, 68, 82, 65, 8 };
 
        System.out.println(Arrays.equals(a, b));
    }
}

fill 填充
使用同一个值,填充整个数组

import java.util.Arrays;
  
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[10];
  
        Arrays.fill(a, 5);
  
        System.out.println(Arrays.toString(a));
  
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值