java的一维数组与二维数组

本文介绍了Java中的数组概念,包括一维数组和二维数组的创建、访问方法,以及如何获取数组长度、使用for循环和增强for循环遍历和对数组进行排序(冒泡排序和选择排序)。
摘要由CSDN通过智能技术生成

1.java数组的概念

             数组是一组相同数据类型元素的集合,是一个容器。

             数组本身是引用数据类型,是一个对象。

             数组可以存储基本数据类型,也可以存储引用数据类型。

             数组创建时必须指定长度,且长度不可变。

2.一维数组的创建

(1):数据类型【】数组名:int [] a;

  (2): 数据类型 数组名字【】:int a [];

import java.util.Arrays;

public class demo03 {
    public static void main(String[] args) {
        int[]b=new int[5];
        System.out.println(b);
        System.out.println(Arrays.toString(b));
        int[]a={1,2,3,4,5,6,7};
        System.out.println(Arrays.toString(a));
        String[]sr=new String[5];
        System.out.println(Arrays.toString(sr));
        boolean[]r=new boolean[10];
        System.out.println(Arrays.toString(r));
    }
}

3.数组的访问

   数组名【索引】 :a[ 0 ];

注意:数组索引从0开始,

           索引数据类型是整数(int)

           索引最大值和数组长度相差一

4.获取数组长度

     数组名.length

5.一维数组的遍历

(1).for循环
int [] b1 = new int []{1,2,3,4,5,6,7};
数组的迭代
for(int i =0;i<b1.length;i++){
 System.out.println(b1[i]);
}
 (2).增强for循环
public class demo01 {
    public static void main(String[] args) {
//增强for循环,适合挨个遍历每一个元素
        //a--要遍历数组
        //int t--声明临时遍历,用来接收每次循环时,从数组取出元素
        //优点,写法简单
        Scanner scanner=new Scanner(System.in);

        int []a=new int[]{1,4,5,6,8,7};
        for(int t : a){
            System.out.println(t);
        }}

6.排序

(1)冒泡排序

//相邻的两个数相比较,每轮将大的数换到后面

 int []a=new int[]{1,4,5,6,8,7};
      
        for(int i=0;i<a.length-1;i++){
            for(int j=0;j<a.length-1-i;j++){
                if(a[j]>a[j+1]){
                    int temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
 (2).选择排序
public class 选择排序 {
    public static void main(String[] args) {
 int a[]=new int[]{4,7,8,9,6,2,1}; //选择排序
 for(int i=0;i< a.length-1;i++){
     int flag=i;
     for(int j=i+1;j<a.length;j++){
         if(a[flag]>a[j]){
             flag=j;
         }
     }
     int temp=a[i];
     a[i]=a[flag];
     a[flag]=temp;
 }
        System.out.println(Arrays.toString(a));
    }
}

7.二维数组

 (1).二维数组的定义

int [ ][ ]a={{1,2,3},{1,2,3},{1,2,3}}

(2).二维数组的创建

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

int [] [] b = {{1,2,3},{1,2,3},{1,2,3}};

int [][] c = new int[3][5];

public class demo02 {
    public static void main(String[] args) {
        // int[][]a=new int[5][5];
        int a[][] = new int[][]{{1, 2, 3, 4, 5}, {4, 5, 6, 7, 8}, {5, 6, 2, 3, 4}, {8, 5, 2, 7, 4}, {9, 6, 3, 5, 2}};
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                System.out.print(a[i][j]);
                System.out.print(" ");
            }
            System.out.println();
        }
        System.out.println("--------------------------");
        int b[][] = new int[3][];
        b[0] = new int[]{1, 2, 3};
        b[1] = new int[]{5, 6, 4, 7, 8, 9};
        b[2] = new int[]{5, 2};
        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.print(" ");
            }
            System.out.println();
        }
    }}
(3). 二维数组的遍历
 int a[][] = new int[][]{{1, 2, 3, 4, 5}, {4, 5, 6, 7, 8}, {5, 6, 2, 3, 4}, {8, 5, 2, 7, 4}, {9, 6, 3, 5, 2}};
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                System.out.print(a[i][j]);
                System.out.print(" ");
            }
            System.out.println();
        }
  • 17
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值