Java中Arrays工具类的用法

本文详细介绍了Java中Arrays类的常用方法,包括asList、copyOf、sort和toString等方法的功能及使用示例。通过实例展示了如何利用这些方法进行数组操作和排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Arrays类是集合中的一个工具类,其中所有的方法全部都是静态方法,下面是其在API文档中的描述,如

asList(T... a) 

Returns a fixed-size list backed by the specified array.

这个方法的返回值是一个List集合,可以用一个List来接收。

copyOf(int[] original, int newLength) 

Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.

这个方法的主要作用是将一个数组复制到另一个数组,而且还有不同的重载方法,但是效果都差不多,只是起始位置和终止位置不同。

sort(int[] a) 

Sorts the specified array into ascending numerical order.

这个方法是排序的方法,可以直接对数组按照自然顺序进行排序,如果参数不是基本数据类型,则需要自定义一个比较器,前提是该比较器要实现Comparator接口,那么方法的描述变为:

sort(T[] a, Comparator<? super T> c)

Sorts the specified array of objects according to the order induced by the specified comparator.

当然,最常用的还是这个方法:

static String toString(int[] a) 

Returns a string representation of the contents of the specified array.

toString方法可以直接将数组转化为字符串格式,这在打印数组的时候非常方便

下面以代码来说明这几个方法:

import java.util.Arrays;

public class TestArray {

	public static void main(String[] args) {
		// 创建一个整数数组a
		int a[] = { 1, 3, 5, 4, 2 };
		// 将整数数组a调用toString()方法打印
		System.out.println("a=" + Arrays.toString(a));
		// 创建数组b,复制a数组的一部分
		int b[] = Arrays.copyOf(a, 3);
		System.out.println("b=" + Arrays.toString(b));
		// 对a调用sort()方法进行自然排序
		Arrays.sort(a);
		System.out.println("排序后的a:" + Arrays.toString(a));
	}

}

这是运行结果:


a=[1, 3, 5, 4, 2]
b=[1, 3, 5]
sort排序后的a:[1, 2, 3, 4, 5]

说明b成功的复制了a的数组

下面我们来测试一下sort(T[] a, Comparator<? super T> c)方法,这个方法的第一个参数不是普通的基本数据类型,而是一个对象,所以需要用到重写的构造器,下面我们以学生对象为例,来按照学号进行排序,这里的学号是以字符串类型创建的:

import java.util.Arrays;
import java.util.Comparator;

public class TestSort {

	public static void main(String[] args) {
		// 创建四个学生类对象
		Student stu1 = new Student("张三", "2017100", 19);
		Student stu2 = new Student("李四", "2017103", 20);
		Student stu3 = new Student("王五", "2017101", 20);
		Student stu4 = new Student("赵六", "2017102", 21);
		// 创建一个数组用来保存学生类对象,并依次赋值
		Student[] stus = new Student[4];
		stus[0] = stu1;
		stus[1] = stu2;
		stus[2] = stu3;
		stus[3] = stu4;

		// 调用Arrays的sort方法对数组内部排序
		Arrays.sort(stus, new StudentComparator());
		for (Student stu : stus) {
			System.out.println(stu.toString());
		}
	}

	
	/**
	 * 定义一个内部类,用来比较Student的大小
	 * @author Administrator
	 *
	 */
	static class StudentComparator implements Comparator<Student> {

		@Override
		public int compare(Student o1, Student o2) {
			if (o1.num.compareTo(o2.num) > 0) {
				return 1;
			} else if (o1.num.compareTo(o2.num) < 0) {
				return -1;
			} else {
				if (o1.age > o2.age) {
					return 1;
				} else if (o1.age < o2.age) {
					return -1;
				}
			}
			return 0;
		}

	}
}
/**
 * 创建学生类对象,属性包括name, num, age
 * @author Administrator
 *
 */
class Student {
	String name;
	String num;
	int age;

	public Student(String name, String num, int age) {
		super();
		this.name = name;
		this.num = num;
		this.age = age;
	}

	
	@Override
	/**
	 * 重写toString()方法
	 */
	public String toString() {
		return "Student [name=" + name + ", num=" + num + ", age=" + age + "]";
	}
}

下面是运行结果:

Student [name=张三, num=2017100, age=19]
Student [name=王五, num=2017101, age=20]
Student [name=赵六, num=2017102, age=21]
Student [name=李四, num=2017103, age=20]

可以看出这里的排序是按照学号的顺序来的,而之前的添加顺序并不是,说明比较器已经生效,这里的比较器是专门用来比较Student类,这个类是一种自定义类,如果需要比较其他的类,则也要实现相应的Comparator<T>的接口。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值