简体   繁体   English

如何使用java.util.Arrays

[英]How to use java.util.Arrays

I'm trying to use the java.util.Arrays class in JavaSE 6 but not sure how i would implement it? 我正在尝试在JavaSE 6中使用java.util.Arrays类,但不确定如何实现它? on an array that I have generated? 在我生成的数组上?

before the start of the class i have 在我开始上课之前

import java.util.Arrays

Java Arrays Java数组

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

Or at instantiation: 或者在实例化时:

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]);
}

For more information, the tutorial from Sun/Oracle will be of great help. 有关更多信息, Sun / Oracle教程将提供很大帮助。 You can also check out the Java language specification on Arrays . 您还可以查看Arrays上Java语言规范

Using the Arrays Utility Class 使用Arrays实用程序类

java.util.Arrays contains a bunch of static methods . java.util.Arrays包含一堆静态方法 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));

Or 要么

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

Well let's say you have an array 好吧,假设你有一个阵列

int[] myArray = new int[] { 3, 4, 6, 8, 2, 1, 9};

And you want to sort it. 而你想要对它进行排序。 You do this: 你做这个:

// assumes you imported at the top
Arrays.sort(myArray);

Here's the whole shebang: 这是整个shebang:

import java.util.Arrays;
class ArrayTest {
    public static void main(String[] args) {
        int[] myArray = new int[] { 3, 4, 6, 8, 2, 1, 9};
        Arrays.sort(myArray);
        System.out.println(Arrays.toString(myArray));
    }
}

And that results in 这导致了

C:\Documents and Settings\glow\My Documents>java ArrayTest
[1, 2, 3, 4, 6, 8, 9]

C:\Documents and Settings\glow\My Documents>

You have not provided enough information about what you are trying to do. 您没有提供有关您尝试执行的操作的足够信息。 java.util.Arrays only exposes static methods, so you simply pass in your array and whatever other params are necessary for the particular method you are calling. java.util.Arrays只暴露静态方法,因此您只需传入您的数组,以及您调用的特定方法所需的任何其他参数。 For instance Arrays.fill(myarray,true) would fill a boolean array with the value true . 例如, Arrays.fill(myarray,true)将填充值为true的布尔数组。

Here is the javadoc for java.util.Arrays 这是java.util.Arrays的javadoc

You can use a static import 您可以使用静态导入

import static java.util.Arrays.*;

int[] ints = {3, 4, 1, 2, 5, 7, 6};
sort(ints);
public static void main(String[] args) {

double array[] = {1.1,2.3,5.6,7.5, 12.2, 44.7,4.25, 2.12};
Arrays.sort(array,1,3); 
for(int i =0;i <array.length;i++){

System.out.println(array[i]);
}
}

result: 结果:

"1.1,2.3,5.6,7.5,12.2,44.7,4.25,2.12" “1.1,2.3,5.6,7.5,12.2,44.7,4.25,2.12”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM