如何在Java中声明和初始化数组?
当前回答
电影类的另一个完整示例:
public class A {
public static void main(String[] args) {
class Movie {
String movieName;
String genre;
String movieType;
String year;
String ageRating;
String rating;
public Movie(String [] str)
{
this.movieName = str[0];
this.genre = str[1];
this.movieType = str[2];
this.year = str[3];
this.ageRating = str[4];
this.rating = str[5];
}
}
String [] movieDetailArr = {"Inception", "Thriller", "MovieType", "2010", "13+", "10/10"};
Movie mv = new Movie(movieDetailArr);
System.out.println("Movie Name: "+ mv.movieName);
System.out.println("Movie genre: "+ mv.genre);
System.out.println("Movie type: "+ mv.movieType);
System.out.println("Movie year: "+ mv.year);
System.out.println("Movie age : "+ mv.ageRating);
System.out.println("Movie rating: "+ mv.rating);
}
}
其他回答
您可以使用数组声明或数组文字(但仅当您立即声明并影响变量时,数组文字不能用于重新分配数组)。
对于基本类型:
int[] myIntArray = new int[3]; // each element of the array is initialised to 0
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 99
int [] myIntArray = IntStream.rangeClosed(0, 100).toArray(); // From 0 to 100
int [] 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]; // each element is initialised to null
String[] myStringArray = {"a", "b", "c"};
String[] myStringArray = new String[]{"a", "b", "c"};
当您首先声明一个数组,然后初始化它,将一个数组作为函数参数传递,或者返回一个数组时,第三种初始化方法非常有用。需要显式类型。
String[] myStringArray;
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};
也是有效的,但我更喜欢在类型后面加括号,因为更容易看出变量的类型实际上是一个数组。
要创建类对象的数组,可以使用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
对于循环:
//repeats for every value in the array
for (ClassName variableName : arrayName){
}
//Note that using this for loop prevents you from editing arrayName
for循环,允许您编辑arrayName(常规for循环):
for (int i = 0; i < arrayName.size(); i++){
//manipulate array here
}
数组是项目的顺序列表
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,以使用新的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为例。有几种方法可以声明和int数组:
int[] i = new int[capacity];
int[] i = new int[] {value1, value2, value3, etc};
int[] i = {value1, value2, value3, etc};
在所有这些中,可以使用inti[]而不是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[]。类型只能在方法参数中使用,因此int.i=newint[]{}不会编译。
请注意,当将int[]传递给方法(或任何其他Type[])时,不能使用第三种方法。在语句int[]i=*{a,b,c,d,etc}*中,编译器假设{…}表示int[]。但这是因为您正在声明一个变量。将数组传递给方法时,声明必须是newType[capartment]或newType[]{…}。
多维数组
多维数组更难处理。本质上,2D阵列是阵列的阵列。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]
推荐文章
- 在JavaScript中根据键值查找和删除数组中的对象
- 如何格式化Joda-Time DateTime仅为mm/dd/yyyy?
- 如何在POM.xml中引用环境变量?
- 如何在android中复制一个文件?
- 如何确定一个数组是否包含另一个数组的所有元素
- 将整数转换为字符串,以逗号表示千
- 接口方法的最终参数-有什么意义?
- Java中的@UniqueConstraint注释
- 给定一个数字数组,返回所有其他数字的乘积的数组(不除法)
- 多维数组如何在内存中格式化?
- 如何在清洁模式下运行eclipse ?如果我们这样做会发生什么?
- 获取java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory异常
- Java中的正则表达式命名组
- c#和Java的主要区别是什么?
- 什么是NullPointerException,我如何修复它?