配列は、インデックスを介して同じオブジェクトのグループにアクセスするためのほとんどのプログラミング言語によって提供される構造体です。いくつかの言語では、型は同じでなければならず(Java)、他の型(JavaScript、Python)では複数の型が配列に含まれることがあります。
配列はほとんどのプログラミング言語で利用でき、しばしばCarray[6]
やVBarray(6)
などの要素にアクセスするためにsquare []
またはround ()
を使用します。
Javaでは、任意のオブジェクトまたはプリミティブ型を配列にすることができます。配列の添え字は、arrayName [index](例えば、 myArray[0]
介してアクセスされます。配列内の値は、myArray [0] = valueを介して設定されます。たとえば、myArrayがString [] myArray[0] = "test";
public class CreateBasicArray{
public static void main(String[] args){
// Creates a new array of Strings, with a length of 1
String[] myStringArray = new String[1];
// Sets the value at the first index of myStringArray to "Hello World!"
myStringArray[0] = "Hello World!";
// Prints out the value at the first index of myStringArray,
// in this case "Hello World!"
System.out.println(myStringArray[0]);
// Creates a new array of ints, with a length of 1
int[] myIntArray = new int[1];
// Sets the value at the first index of myIntArray to 1
myIntArray[0] = 1;
// Prints out the value at the first index of myIntArray,
// in this case 1
System.out.println(myIntArray[0]);
// Creates a new array of Objects with a length of 1
Object[] myObjectArray = new Object[1];
// Constructs a new Java Object, and sets the value at the first
// index of myObjectArray to the new Object.
myObjectArray[0] = new Object();
}
}