如何在Java中声明和初始化数组?


当前回答

根据数组的定义,数组可以包含基元数据类型以及类的对象。对于基元数据类型,实际值存储在相邻的内存位置。对于类的对象,实际对象存储在堆段中。

一维阵列:

一维数组声明的一般形式是

type var-name[];
OR
type[] var-name;

在Java中实例化数组

var-name = new type [size];

例如

int intArray[];  // Declaring an array
intArray = new int[20];  // Allocating memory to the array

// The below line is equal to line1 + line2
int[] intArray = new int[20]; // Combining both statements in one
int[] intArray = new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// Accessing the elements of the specified array
for (int i = 0; i < intArray.length; i++)
    System.out.println("Element at index " + i + ": "+ intArray[i]);

参考:Java中的数组

其他回答

此外,如果您需要更动态的内容,可以使用List界面。这不会表现得很好,但更灵活:

List<String> listOfString = new ArrayList<String>();

listOfString.add("foo");
listOfString.add("bar");

String value = listOfString.get(0);
assertEquals( value, "foo" );

要创建类对象的数组,可以使用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[] array = new int[n]; // "n" being the number of spaces to allocate in the array

对于一个初始化的数组:

int[] array = {1,2,3,4 ...};

您还可以创建多维数组,如下所示:

int[][] array2d = new int[x][y]; // "x" and "y" specify the dimensions
int[][] array2d = { {1,2,3 ...}, {4,5,6 ...} ...};

电影类的另一个完整示例:

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);
    }
}
Type[] variableName = new Type[capacity];

Type[] variableName = {comma-delimited values};



Type variableName[] = new Type[capacity]; 

Type variableName[] = {comma-delimited values};

也是有效的,但我更喜欢在类型后面加括号,因为更容易看出变量的类型实际上是一个数组。