【Java语法基础】3.数组

3.数组

一维数组

声明
package org.example;
public class Main{
    public static void main(String[] args) throws Exception
    {
        int[] a = new int[5];
        float[] b = new float[10];
        char[] c = new char[3];
        double[] d = new double[4];
    }
}
初始化
package org.example;
public class Main{
    public static void main(String[] args) throws Exception
    {
        int[] a = new int[5]; //未初始化,则为0
        double[] b = {1.0, 2.0, 3.0};
        char[] c = {'a', 'b', 'c'};
        //也可:
        double d[] = {1.0, 2.0, 3.0};
    }
}

多维数组

声明与初始化

二维数组即数组元素为一维数组。注意:一维数组的长度不一定要相同,见下面d变量,

package org.example;
public class Main{
    public static void main(String[] args) throws Exception
    {
        //声明
        int[][] a = new int[3][4];
        int[][][] b = new int[3][4][5];

        //初始化
        int[][] c = {
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}
        };
		//注意:可以不一样长
        int[][] d = {
                {1, 2, 3, 4},
                {5, 6, 7},
                {8, 9, 10, 11}
        };
    }
}
范围遍历
package org.example;
public class Main{
    public static void main(String[] args) throws Exception
    {
        int[][] d = {
                {1, 2, 3, 4},
                {5, 6, 7},
                {8, 9, 10, 11}
        };
        //范围遍历
        for(int[] row : d)
        {
            //此处的row变量是一个数组了
            for(int x : row)
            {
                System.out.printf("%d ", x);
            }
            System.out.println("");
        }
    }
}
常用API
  • 属性length:获取数组长度

以下Arrays相关函数注意需要:

import java.util.Arrays;
  • Arrays.sort():数组排序

  • Arrays.toString():数组转化为字符串

  • Arrays.deepToString():多维数组转化为字符串

  • Array.fill(int[] a, int val):填充数组

示例如下:

package org.example;
import java.util.Arrays;

public class Main{
    public static void main(String[] args) throws Exception
    {
        int[][] d = {
                {7, 8, 1, 2, 10},
                {5, 2, 0},
                {6, 7, 10, 1}
        };
        //可以自定义排序方式,使用匿名函数
        Arrays.sort(d, (x, y) -> {
            return x[0] - y[0];
        });
        //多维数组输出
        System.out.println(Arrays.deepToString(d));
        //一维数组输出
        System.out.println(Arrays.toString(d[0]));
    }
}

下列补充一下自定义排序:

import java.util.Scanner;
import java.util.Arrays;
//需要实现该接口
class Group implements Comparable<Group>
{
    private int x;
    private double y;
    private String z;
    public Group(int x, double y, String z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    public void print()
    {
        System.out.printf("%d %.2f %s\n", x, y, z);
    }
	
    //需要实现这个函数才能排序
    //小于返回负数
    public int compareTo(Group g)
    {
        return x - g.x;
    }
}

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Group[] gs = new Group[n];
        
        for(int i = 0; i < n; i ++)
        {
            int x = sc.nextInt();
            double y = sc.nextDouble();
            String z = sc.next();
            
            gs[i] = new Group(x, y, z);
        }
        
        //使用Arrays排序
        Arrays.sort(gs);
        for(int i = 0; i < n; i ++)
            gs[i].print();
        
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

指针常量

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值