java处理json数组_用Java处理数组 小鱼儿 2022-12-06 15:35 178阅读 0赞 java处理json数组 If a program needs to work with a number of values of the same [data type][], you could declare a [variable][] for each number. For example, a program that displays lottery numbers: 如果程序需要使用多个相同[数据类型][data type]的值,则可以为每个数字声明一个[变量][variable] 。 例如,显示彩票号码的程序: int lotteryNumber1 = 16; int lotteryNumber2 = 32; int lotteryNumber3 = 12; int lotteryNumber4 = 23; int lotteryNumber5 = 33; int lotteryNumber6 A more elegant way of dealing with values which can be grouped together is to use an array. An array is a container that holds a fixed number of values of a data type. In the above example, the lottery numbers could be grouped together in an int array: 处理可以分组在一起的值的一种更优雅的方法是使用数组。 数组是保存固定数量的数据类型值的容器。 在上面的示例中,彩票号码可以组合为一个int数组: int[] lotteryNumbers = {16,32,12,23,33,20}; Think of an array as a row of boxes. The number of boxes in the array cannot change. Each box can hold a value as long as it is of the same data type as the values contained within the other boxes. You can look inside a box to see what value it contains or replace the contents of the box with another value. When talking about arrays, the boxes are called elements. 将数组想像成一排盒子。 阵列中的盒子数不能更改。 每个框都可以保存一个值,只要它与其他框中包含的值具有相同的数据类型即可。 您可以查看框内的内容以查看其包含的值,也可以将框的内容替换为另一个值。 当谈论数组时,这些盒子称为元素。 ## 声明和初始化数组 **(** Declaring and Initializing an Array **)** ## The declaration statement for an array is similar to the one used to [declare any other variable][]. It contains the data type followed by the name of the array - the only difference is the inclusion of square brackets next to the data type: 数组的声明语句类似于用于[声明任何其他变量][declare any other variable]的声明语句。 它包含数据类型,后跟数组名称-唯一的区别是数据类型旁边包含方括号: int[] intArray; float[] floatArray; char[] char The [declaration statements][] above tell the compiler that 上面的[声明语句][declaration statements]告诉编译器 intArrayvariable is an array of ints, floatArrayis an array of floatsand charArrayis an array of chars. Like any variable, they cannot be used until it has been initialized by assigning it a value. For an array the assignment of a value to an array must define the size of an ar intArray = new int[10]; The number inside the brackets defines how many elements the array holds. The above assignment statement creates an int array with ten elements. Of course, there's no reason why the declaration and assignment can't happen in one statement: 方括号内的数字定义数组包含的元素数。 上面的赋值语句创建一个包含10个元素的int数组。 当然,没有理由不能在一个语句中进行声明和赋值: float[] floatArray = new float[10]; Arrays are not limited to primitive data types. Arrays of objects can be created: 数组不限于原始数据类型。 可以创建对象数组: String[] names = new String[5]; ## 使用数组 **(** Using an Array **)** ## Once an array has been initialized the elements can have values assigned to them by using the array's index. The index defines the position of each element in the array. The first element is at 0, the second element at 1 and so on. It's important to note that the index of the first element is 0. It's easy to think that because an array has ten elements that the index is from 1 to 10 instead of from 0 to 9. For example, if we go back to the lottery numbers example we can create an array containing 6 elements and assign the lottery numbers to the elements: 初始化数组后,可以使用数组的索引为元素分配值。 索引定义数组中每个元素的位置。 第一个元素为0,第二个元素为1,依此类推。 重要的是要注意,第一个元素的索引是0。很容易想到,因为一个数组有十个元素,所以索引是从1到10而不是从0到9。例如,如果我们回到彩票数字示例,我们可以创建一个包含6个元素的数组,然后将彩票编号分配给这些元素: int[] lotteryNumbers = new int[6]; lotteryNumbers[0] = 16; lotteryNumbers[1] = 32; lotteryNumbers[2] = 12; lotteryNumbers[3] = 23; lotteryNumbers[4] = 33; lotteryNumbers[5] There is a shortcut to filling elements in an array by putting the values for the elements in the declaration statement: 通过将元素的值放在声明语句中,可以快速填充数组中的元素: int[] lotteryNumbers = {16,32,12,23,33,20}; String[] names = {"John", "James", "Julian", "Jack", "Jonathon& The values for each element is placed inside a pair of curly brackets. The order of the values determines which element is assigned the value starting with index position 0. The number of elements in the array is determined by the number of values inside the curly brackets. 每个元素的值放在一对大括号内。 值的顺序确定从索引位置0开始为哪个元素分配值。数组中的元素数由大括号内的值数确定。 To get the value of an element its index is used: 要获取元素的值,请使用其索引: System.out.println("The value of the first element is " + lotteryNumbers[0]); To find out how many elements an array has use the length field: 要找出一个数组有多少个元素,请使用length字段: System.out.println("The lotteryNumbers array has " + lotteryNumbers.length + " elements"); **Note:** A common mistake when using the length method is to forget is to use the length value as an index position. This will always result in an error as the index positions of an array are 0 to length - 1. **注意:**使用长度方法时,一个常见的错误是忘记使用长度值作为索引位置。 由于数组的索引位置从0到length-1,这总是会导致错误。 ## 多维数组 **(** Multidimensional Arrays **)** ## The arrays we have been looking at so far are known as one-dimensional (or single dimensional) arrays. This means they only have one row of elements. However, arrays can have more than one dimension. A multidimensional is actually an array that contains arrays: 到目前为止,我们一直在研究的数组称为一维(或一维)数组。 这意味着它们只有一行元素。 但是,数组可以具有多个维度。 多维实际上是包含数组的数组: int[][] lotteryNumbers = { {16,32,12,23,33,20},{34,40,3,11,33,24}}; The index for a multidimensional array consists of two numbers: 多维数组的索引由两个数字组成: System.out.println("The value of element 1,4 is " + lotteryNumbers[1][4]); Although the length of the arrays contained within a multidimensional array do not have to be the same length: 尽管多维数组中包含的数组的长度不必一定是相同的长度: String[][] names = new String[5][7]; ## 复制数组 **(** Copying an Array **)** ## To copy an array the easiest way is to use the 复制数组最简单的方法是使用 arraycopymethod of the System class. The arraycopymethod can be used to copy all the elements of an array or a subsection of them. There are five parameters passed to the arraycopymethod - the original array, the index position to start copying an element from, the new array, the index position to start inserting from, the number of elements to c public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) For example, to create a new array containing the last four elements of an 例如,要创建一个新数组,其中包含一个数组的最后四个元素 int ar int[] lotteryNumbers = {16,32,12,23,33,20}; int[] newArrayNumbers = new int[4]; System.arraycopy(lotteryNumbers, 2, newArrayNumbers, As arrays are a fixed length the 由于数组是固定长度的 arraycopymethod can be a useful way to change the size of an ar To further your knowledge about arrays you can learn about manipulating arrays using the Arrays class and making dynamic arrays (i.e., arrays when the number of elements is not a fixed number) using the [ArrayList class][]. 为了进一步了解数组,您可以学习使用Arrays类来处理数组,以及使用[ArrayList类][ArrayList class]来创建动态数组(即,元素数不是固定数时的[数组)][ArrayList class] 。 > 翻译自: [https://www.thoughtco.com/working-with-arrays-2034318][https_www.thoughtco.com_working-with-arrays-2034318] java处理json数组 [data type]: https://www.thoughtco.com/primitive-data-types-2034320 [variable]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html [declare any other variable]: https://www.thoughtco.com/declaring-variables-2034319 [declaration statements]: https://www.thoughtco.com/declaration-statement-2034076 [ArrayList class]: https://www.thoughtco.com/using-the-arraylist-2034204 [https_www.thoughtco.com_working-with-arrays-2034318]: https://www.thoughtco.com/working-with-arrays-2034318
还没有评论,来说两句吧...