如何使用java.util.Arrays [英] How to use java.util.Arrays

查看:98
本文介绍了如何使用java.util.Arrays的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JavaSE 6中使用java.util.Arrays类但不确定如何实现它?在我生成的数组上?

I'm trying to use the java.util.Arrays class in JavaSE 6 but not sure how i would implement it? on an array that I have generated?

在课程开始之前我有

import java.util.Arrays


推荐答案

Java数组



要声明一个整数数组,请从以下开始:

Java Arrays

To declare an array of integers, you start with:

int[] myArray;

要实例化十个整数的数组,您可以尝试:

To instantiate an array of ten integers, you can try:

myArray = new int[10];

要设置该数组中的值,请尝试:

To set values in that array, try:

myArray[0] = 1; // arrays indices are 0 based in Java

或者在实例化时:

int[] myArray2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

要从数组中获取值,请尝试:

To get values from the array, try:

System.out.println(myArray[0]);

要打印数组中的所有值,请尝试:

To print all the values in an array, try:

// go from 0 to one less than the array length, based on 0 indexing
for(int i = 0; i < myArray2.length; i++) {
    System.out.println(myArray2[i]);
}

有关详细信息,请来自Sun / Oracle的教程将会有很大帮助。您还可以查看 Arrays上的Java语言规范

For more information, the tutorial from Sun/Oracle will be of great help. You can also check out the Java language specification on Arrays.

java.util.Arrays 包含一堆静态方法。静态方法属于该类,并且不需要该类的实例才能被调用。相反,它们以类名作为前缀来调用。

java.util.Arrays contains a bunch of static methods. Static methods belong to the class and do not require an instance of the class in order to be called. Instead they are called with the class name as a prefix.

所以您可以执行以下操作:

So you can do things like the following:

// print a string representation of an array
int[] myArray = {1, 2, 3, 4};
System.out.println(Arrays.toString(myArray));

// sort a list
int[] unsorted = {3, 4, 1, 2, 5, 7, 6};
Arrays.sort(unsorted);

这篇关于如何使用java.util.Arrays的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆