int[] myIntArray = new int[3];int[] myIntArray = {1, 2, 3};int[] myIntArray = new int[]{1, 2, 3};
// Since Java 8. Doc of IntStream: https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html
int [] myIntArray = IntStream.range(0, 100).toArray(); // From 0 to 99int [] myIntArray = IntStream.rangeClosed(0, 100).toArray(); // From 0 to 100int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).toArray(); // The order is preserved.int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).sorted().toArray(); // Sort
对于类,例如String,它是相同的:
String[] myStringArray = new String[3];String[] myStringArray = {"a", "b", "c"};String[] myStringArray = new String[]{"a", "b", "c"};
Type[] variableName = new Type[capacity];
Type[] variableName = {comma-delimited values};
Type variableName[] = new Type[capacity];
Type variableName[] = {comma-delimited values};
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 notallowed supertype of Horse nor other subtype of Animal.*/Horse[] h1 = new Horse[10];h1[0] = new Animal(); // Not allowedh1[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() }};
String [] a = { "hello", "world" };// is equivalent toString [] a = { new String({'h','e','l','l','o'}), new String({'w','o','r','l','d'}) };
Integer [] b = { 1234, 5678 };// is equivalent toInteger [] 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;
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};
这一次不需要在框括号中提及大小。甚至是一个简单的变体:
int[] intArray = {1, 2, 3, 4};
An array of length 0
int[] intArray = new int[0];int length = intArray.length; // Will return length 0
类似于多维数组
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 2int length2 = intArray[0].length; // Will return 3
Using box brackets before the variable:
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.NumbernumArray[0] = new Float(1.5f); // java.lang.FloatnumArray[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
int intArray[]; // Declaring an arrayintArray = new int[20]; // Allocating memory to the array
// The below line is equal to line1 + line2int[] intArray = new int[20]; // Combining both statements in oneint[] intArray = new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Accessing the elements of the specified arrayfor (int i = 0; i < intArray.length; i++)System.out.println("Element at index " + i + ": "+ intArray[i]);
int[] nums1; // best practiceint []nums2;int nums3[];
多维数组
int[][] nums1; // best practiceint [][]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()};
循环for
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(自Java8)
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(自Java8)
int size = 3;int randomNumberOrigin = -10;int randomNumberBound = 10int[] nums = new Random().ints(size, randomNumberOrigin, randomNumberBound).toArray();
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}}