我该如何在Java中声明和初始化数组?
我该如何在Java中声明和初始化数组?
在Java 8中,您可以使用类似于以下内容的东西。
String[] strs = IntStream.range(0, 15) // 15 is the size
.mapToObj(i -> Integer.toString(i))
.toArray(String[]::new);
以原始数据类型int
为例,有几种声明int
数组的方法:
int[] i = new int[capacity];
int[] i = new int[] {value1, value2, value3, etc};
int[] i = {value1, value2, value3, etc};
在这些情况下,你可以使用 int i[]
替代 int[] i
。
使用反射,你可以使用 (Type[]) Array.newInstance(Type.class, capacity);
请注意,在方法参数中,...
表示可变参数
。实际上,任何数量的参数都可以。以下代码更容易理解:
public static void varargs(int fixed1, String fixed2, int... varargs) {...}
...
varargs(0, "", 100); // fixed1 = 0, fixed2 = "", varargs = {100}
varargs(0, "", 100, 200); // fixed1 = 0, fixed2 = "", varargs = {100, 200};
在这个方法中,varargs
被作为普通的 int[]
处理。Type...
只能用在方法参数中,所以 int... i = new int[] {}
会编译错误。int[]
(或任何其他的 Type[]
)时,不能使用第三种方式。在语句 int[] i = *{a, b, c, d, etc}*
中,编译器会假设 {...}
意味着一个 int[]
,但那是因为你正在声明一个变量。当向方法传递数组时,声明必须是 new Type[capacity]
或者 new Type[] {...}
。
多维数组更难处理。实际上,一个二维数组就是一个数组的数组。int[][]
表示 int[]
的数组。关键在于如果一个 int[][]
被声明为 int[x][y]
,最大的索引是 i[x-1][y-1]
。实质上,一个矩形的 int[3][5]
是:
[0, 0] [1, 0] [2, 0]
[0, 1] [1, 1] [2, 1]
[0, 2] [1, 2] [2, 2]
[0, 3] [1, 3] [2, 3]
[0, 4] [1, 4] [2, 4]
int size = 3;
int[] intArray = (int[]) Array.newInstance(int.class, size );
声明一个对象引用的数组:
class Animal {}
class Horse extends Animal {
public static void main(String[] args) {
/*
* Array of Animal can hold Animal and Horse (all subtypes of Animal allowed)
*/
Animal[] a1 = new Animal[10];
a1[0] = new Animal();
a1[1] = new Horse();
/*
* Array of Animal can hold Animal and Horse and all subtype of Horse
*/
Animal[] a2 = new Horse[10];
a2[0] = new Animal();
a2[1] = new Horse();
/*
* Array of Horse can hold only Horse and its subtype (if any) and not
allowed supertype of Horse nor other subtype of Animal.
*/
Horse[] h1 = new Horse[10];
h1[0] = new Animal(); // Not allowed
h1[1] = new Horse();
/*
* This can not be declared.
*/
Horse[] h2 = new Animal[10]; // Not allowed
}
}
数组是一串项目的有序列表。
int item = value;
int [] one_dimensional_array = { value, value, value, .., value };
int [][] two_dimensional_array =
{
{ value, value, value, .. value },
{ value, value, value, .. value },
.. .. .. ..
{ value, value, value, .. value }
};
如果它是一个物体,那么它就是相同的概念。Object item = new Object();
Object [] one_dimensional_array = { new Object(), new Object(), .. new Object() };
Object [][] two_dimensional_array =
{
{ new Object(), new Object(), .. new Object() },
{ new Object(), new Object(), .. new Object() },
.. .. ..
{ new Object(), new Object(), .. new Object() }
};
对于对象而言,您需要将其指定为null
,或使用new Type(...)
初始化它们,类似String
和Integer
的类是特殊情况,将按以下方式处理。
String [] a = { "hello", "world" };
// is equivalent to
String [] a = { new String({'h','e','l','l','o'}), new String({'w','o','r','l','d'}) };
Integer [] b = { 1234, 5678 };
// is equivalent to
Integer [] b = { new Integer(1234), new Integer(5678) };
一般情况下,您可以创建 M
维的数组。
int [][]..[] array =
// ^ M times [] brackets
{{..{
// ^ M times { bracket
// this is array[0][0]..[0]
// ^ M times [0]
}}..}
// ^ M times } bracket
;
值得注意的是,创建一个 M
维数组在空间方面是昂贵的。因为当你创建一个每个维度上都有 N
的 M
维数组时,数组的总大小比 N^M
还要大,因为每个数组都有一个引用,在 M 维中存在一个 (M-1) 维的引用数组。总大小如下所示:
Space = N^M + N^(M-1) + N^(M-2) + .. + N^0
// ^ ^ array reference
// ^ actual data
int[] nums1; // best practice
int []nums2;
int nums3[];
int[][] nums1; // best practice
int [][]nums2;
int[] []nums3;
int[] nums4[];
int nums5[][];
int[] nums = new int[3]; // [0, 0, 0]
Object[] objects = new Object[3]; // [null, null, null]
int[] nums1 = {1, 2, 3};
int[] nums2 = new int[]{1, 2, 3};
Object[] objects1 = {new Object(), new Object(), new Object()};
Object[] objects2 = new Object[]{new Object(), new Object(), new Object()};
int[] nums = new int[3];
for (int i = 0; i < nums.length; i++) {
nums[i] = i; // can contain any YOUR filling strategy
}
Object[] objects = new Object[3];
for (int i = 0; i < objects.length; i++) {
objects[i] = new Object(); // can contain any YOUR filling strategy
}
for
和 Random
循环int[] nums = new int[10];
Random random = new Random();
for (int i = 0; i < nums.length; i++) {
nums[i] = random.nextInt(10); // random int from 0 to 9
}
Stream
(自Java 8以来)int[] nums1 = IntStream.range(0, 3)
.toArray(); // [0, 1, 2]
int[] nums2 = IntStream.rangeClosed(0, 3)
.toArray(); // [0, 1, 2, 3]
int[] nums3 = IntStream.of(10, 11, 12, 13)
.toArray(); // [10, 11, 12, 13]
int[] nums4 = IntStream.of(12, 11, 13, 10)
.sorted()
.toArray(); // [10, 11, 12, 13]
int[] nums5 = IntStream.iterate(0, x -> x <= 3, x -> x + 1)
.toArray(); // [0, 1, 2, 3]
int[] nums6 = IntStream.iterate(0, x -> x + 1)
.takeWhile(x -> x < 3)
.toArray(); // [0, 1, 2]
int size = 3;
Object[] objects1 = IntStream.range(0, size)
.mapToObj(i -> new Object()) // can contain any YOUR filling strategy
.toArray(Object[]::new);
Object[] objects2 = Stream.generate(() -> new Object()) // can contain any YOUR filling strategy
.limit(size)
.toArray(Object[]::new);
Random
和 Stream
int size = 3;
int randomNumberOrigin = -10;
int randomNumberBound = 10
int[] nums = new Random().ints(size, randomNumberOrigin, randomNumberBound).toArray();
int[][] nums = new int[3][3]; // [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
Object[][] objects = new Object[3][3]; // [[null, null, null], [null, null, null], [null, null, null]]
int[][] nums1 = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int[][] nums2 = new int[][]{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Object[][] objects1 = {
{new Object(), new Object(), new Object()},
{new Object(), new Object(), new Object()},
{new Object(), new Object(), new Object()}
};
Object[][] objects2 = new Object[][]{
{new Object(), new Object(), new Object()},
{new Object(), new Object(), new Object()},
{new Object(), new Object(), new Object()}
};
for
循环int[][] nums = new int[3][3];
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[i].length; i++) {
nums[i][j] = i + j; // can contain any YOUR filling strategy
}
}
Object[][] objects = new Object[3][3];
for (int i = 0; i < objects.length; i++) {
for (int j = 0; j < nums[i].length; i++) {
objects[i][j] = new Object(); // can contain any YOUR filling strategy
}
}
java.util.Arrays
,你可以这样做:List<String> number = Arrays.asList("1", "2", "3");
// Out: ["1", "2", "3"]
这个很简单和直接创建一个 List。(List 和 Array 之间的区别是什么?)现在如果你真的想要一个 Array
,你仍然可以使用:
String[] numberArray = Arrays.asList("1", "2", "3").toArray(new String[0]);
声明并初始化Java 8及更高版本。创建一个简单的整数数组:
int [] a1 = IntStream.range(1, 20).toArray();
System.out.println(Arrays.toString(a1));
// Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
int [] a2 = new Random().ints(15, -50, 50).toArray();
double [] a3 = new Random().doubles(5, 0, 1e17).toArray();
幂次数序列:
double [] a4 = LongStream.range(0, 7).mapToDouble(i -> Math.pow(2, i)).toArray();
System.out.println(Arrays.toString(a4));
// Output: [1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0]
对于 String[],您必须指定一个构造函数:
String [] a5 = Stream.generate(()->"I will not squeak chalk").limit(5).toArray(String[]::new);
System.out.println(Arrays.toString(a5));
String [][] a6 = List.of(new String[]{"a", "b", "c"} , new String[]{"d", "e", "f", "g"})
.toArray(new String[0][]);
System.out.println(Arrays.deepToString(a6));
// Output: [[a, b, c], [d, e, f, g]]
要创建类对象的数组,您可以使用 java.util.ArrayList
来定义一个数组:
public ArrayList<ClassName> arrayName;
arrayName = new ArrayList<ClassName>();
arrayName.add(new ClassName(class parameters go here);
从数组中读取:
ClassName variableName = arrayName.get(index);
variableName
是对数组的引用,这意味着操作 variableName
将操作 arrayName
for 循环://repeats for every value in the array
for (ClassName variableName : arrayName){
}
//Note that using this for loop prevents you from editing arrayName
允许您编辑arrayName
的for循环(传统的for循环):
for (int i = 0; i < arrayName.size(); i++){
//manipulate array here
}
这里有很多答案。我添加了一些创建数组的巧妙方法(从考试角度来看,了解这个是很好的)。
Declare and define an array
int intArray[] = new int[3];
This will create an array of length 3. As it holds a primitive type, int, all values are set to 0 by default. For example,
intArray[2]; // Will return 0
Using box brackets [] before the variable name
int[] intArray = new int[3];
intArray[0] = 1; // Array content is now {1, 0, 0}
Initialise and provide data to the array
int[] intArray = new int[]{1, 2, 3};
This time there isn't any need to mention the size in the box bracket. Even a simple variant of this is:
int[] intArray = {1, 2, 3, 4};
An array of length 0
int[] intArray = new int[0];
int length = intArray.length; // Will return length 0
Similar for multi-dimensional arrays
int intArray[][] = new int[2][3];
// This will create an array of length 2 and
//each element contains another array of length 3.
// { {0,0,0},{0,0,0} }
int lenght1 = intArray.length; // Will return 2
int length2 = intArray[0].length; // Will return 3
在变量之前使用方括号:
int[][] intArray = new int[2][3];
如果您在末尾放置一个方括号,也完全没问题:
int[] intArray [] = new int[2][4];
int[] intArray[][] = new int[2][3][4]
一些例子
int [] intArray [] = new int[][] {{1,2,3},{4,5,6}};
int [] intArray1 [] = new int[][] {new int[] {1,2,3}, new int [] {4,5,6}};
int [] intArray2 [] = new int[][] {new int[] {1,2,3},{4,5,6}}
// All the 3 arrays assignments are valid
// Array looks like {{1,2,3},{4,5,6}}
并非每个内部元素的大小都是强制性的。
int [][] intArray = new int[2][];
intArray[0] = {1,2,3};
intArray[1] = {4,5};
//array looks like {{1,2,3},{4,5}}
int[][] intArray = new int[][2] ; // This won't compile. Keep this in mind.
如果您使用上述语法,请确保在正向方向上指定方括号中的值。否则它将无法编译。以下是一些示例:
int [][][] intArray = new int[1][][];
int [][][] intArray = new int[1][2][];
int [][][] intArray = new int[1][2][3];
另一个重要的特性是协变
Number[] numArray = {1,2,3,4}; // java.lang.Number
numArray[0] = new Float(1.5f); // java.lang.Float
numArray[1] = new Integer(1); // java.lang.Integer
// You can store a subclass object in an array that is declared
// to be of the type of its superclass.
// Here 'Number' is the superclass for both Float and Integer.
Number num[] = new Float[5]; // This is also valid
重要提示:对于引用类型,在数组中存储的默认值为null。