首页 > 未分类 > Array数组
2017
07-16

Array数组

Name

Array数组

   

Examples

int[] numbers = new int[3];

numbers[0] = 90; // Assign value to first element in the array

numbers[1] = 150; // Assign value to second element in the array

numbers[2] = 30; // Assign value to third element in the array

int a = numbers[0] + numbers[1]; // Sets variable ‘a’ to 240

int b = numbers[1] + numbers[2]; // Sets variable ‘b’ to 180

Array数组 - 第1张  | Processing编程艺术

int[] numbers = { 90, 150, 30 }; // Alternate syntax

int a = numbers[0] + numbers[1]; // Sets variable ‘a’ to 240

int b = numbers[1] + numbers[2]; // Sets variable ‘b’ to 180

Array数组 - 第2张  | Processing编程艺术

int degrees = 360;

float[] cos_vals = new float[degrees];

// Use a for() loop to quickly iterate

// through all values in an array.

for (int i=0; i < degrees; i++) {

cos_vals[i] = cos(TWO_PI/degrees * i);

}

Array数组 - 第3张  | Processing编程艺术

float[] randoms = new float[100];

for (int i = 0; i < randoms.length; i++) {

randoms[i] = random(100);

}

 

// You can also use an enhanced loop

// to access the elements of an array

for (float val : randoms) {

println(val);

}

 

// This works with arrays of objects, too,

// but not when first making the array

PVector[] vectors = new PVector[5];

for (int i = 0; i < vectors.length; i++) {

vectors[i] = new PVector();

}

 

// The syntax only applies when iterating

// over an existing array

for (PVector v : vectors) {

point(v.x, v.y);

}

Description

描述

An array is a list of data. It is possible to have an array of any type of data. Each piece of data in an array is identified by an index number representing its position in the array. The first element in the array is [0], the second element is [1], and so on. Arrays are similar to objects, so they must be created with the keyword new.

Each array has a variable length, which is an integer value for the total number of elements in the array. Note that since index numbering begins at zero (not 1), the last value in an array with a lengthof 5 should be referenced as array[4] (that is, the length minus 1), not array[5], which would trigger an error.

Another common source of confusion is the difference between using length to get the size of an array and length() to get the size of a String. Notice the presence of parentheses when working with Strings. (array.length is a variable, while String.length() is a method specific to the String class.)

数组是数据的列表。可以有任何类型的数据的数组。数组中的每一个数据都由表示其在数组中位置的索引号标识。数组中的第一个元素是 [0], 第二个元素是 [1], 等等。数组与对象相似, 因此必须使用 new 关键字创建它们。

 

每个数组都有一个可变长度, 它是数组中元素总数的整数值。请注意, 由于索引编号从零开始 (不是 1), 因此, 5的数组中的最后一个值应被引用为数组 [4] (即长度减去 1), 而不是数组 [5], 这将触发错误。

 

另一个常见的混淆来源是使用长度来获取数组的大小和长度 () 以获取字符串的大小之间的差异。使用字符串时请注意括号的存在。(数组. length 是一个变量, 而字符串. length () 是特定于字符串类的方法。

Syntax

语法

datatype[] var

var[element] = value

var.length

Parameters

参数

Datatype

数据类型

any primitive or compound datatype, including user-defined classes

任何基元或复合数据类型, 包括用户定义的类

var

any valid variable name

任何有效的变量名

Element

元素

int: must not exceed the length of the array minus 1

不能超过数组的长度减去1

Value

data to assign to the array element; must be the same datatype as the array

要分配给数组元素的数据;必须与数组的数据类型相同



最后编辑:
作者:卡萨布兰卡
这个作者貌似有点懒,什么都没有留下。

留下一个回复

你的email不会被公开。