在java中使用数组

如果程序需要处理多个相同数据类型的值,可以为每个值声明一个变量。例如,显示彩票号码的程序:...

如果程序需要处理多个相同数据类型的值,可以为每个值声明一个变量。例如,显示彩票号码的程序:

Young Developer Working In His Office. int lotteryNumber1 = 16; int lotteryNumber2 = 32; int lotteryNumber3 = 12; int lotteryNumber4 = 23; int lotteryNumber5 = 33;

处理可以分组在一起的值的更优雅的方法是使用数组。数组是一个容器,它包含固定数量的数据类型值。在上面的示例中,彩票号码可以组合在一个整数数组中:

int[] lotteryNumbers = {16,32,12,23,33,20};

将数组视为一行框。数组中的框数不能更改。每个框可以保存一个值,只要它与其他框中包含的值具有相同的数据类型。您可以查看框内的值,或者用另一个值替换框中的内容。当谈到数组时,这些框称为元素。

声明和初始化数组

数组的声明语句类似于用于声明任何其他变量的声明语句。它包含后跟数组名称的数据类型-唯一的区别是在数据类型旁边包含方括号:

int[] intArray; float[] floatArray;

上面的声明语句告诉编译器

intArrayvariable is an array of ints, floatArrayis an array of floatsand charArray 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: 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: int[] lotteryNumbers = new int[6]; lotteryNumbers[0] = 16; lotteryNumbers[1] = 32; lotteryNumbers[2] = 12; lotteryNumbers[3] = 23; lotteryNumbers[4] = 33; 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}; 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. 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: 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.

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 arraycopy method of the System class. The arraycopy method can be used to copy all the elements of an array or a subsection of them. There are five parameters passed to the arraycopy 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 int[] lotteryNumbers = {16,32,12,23,33,20}; int[] newArrayNumbers = new int[4]; As arrays are a fixed length the arraycopy 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.

  • 发表于 2021-10-01 03:04
  • 阅读 ( 221 )
  • 分类:编程

你可能感兴趣的文章

java异常:您处理的对吗?

...方更好地处理而不是在遇到异常情况的地方处理时,可以使用它。考虑以下示例: ...

  • 发布于 2021-03-13 09:11
  • 阅读 ( 253 )

如何使用java arraylist

...其他语言中数组通常需要的大部分功能。这些操作包括:使用索引访问元素、添加、删除和更新元素、动态重新调整大小、对元素进行迭代等。这些操作中的大多数都经过了专门调整,以用于一般用途。 ...

  • 发布于 2021-03-13 20:41
  • 阅读 ( 202 )

如何用java编写for循环

...相互作用的。可视化嵌套for循环工作方式的一个好方法是使用嵌套for循环创建以下模式。 ...

  • 发布于 2021-03-29 11:52
  • 阅读 ( 228 )

列表(list)和数组表(arraylist)的区别

...ist是泛型的。Arraylist是特定的,两者可以替换,但不推荐使用。这是最推荐的syntax:List list =新建ArrayList();你是一个工作的程序员吗?你有一个更简单的方法来解释这个吗?让我们在评论中知道! 
 ...

  • 发布于 2021-06-24 03:08
  • 阅读 ( 213 )

数组(array)和一串(string)的区别

...意味着它可以容纳固定数量的单一类型的值。C中不允许使用可变大小的数组。一旦分配了一个数组,它的大小就固定了。字符串的大小是可变的,这意味着如果它是char指针,则可以更改它。 数组与字符串:比较图 总结 - 数组...

  • 发布于 2021-06-25 10:17
  • 阅读 ( 1387 )

数组表(arraylist)和矢量(vector)的区别

...术语 数组列表,向量 什么是数组表(arraylist)? ArrayList是使用ArrayList类实现的数据结构。这个ArrayList类进一步实现了List接口。对于阵列来说,它是一个更好的选择。通常数组的长度是固定的。因此,程序员添加的元素不能超过...

  • 发布于 2021-07-01 09:19
  • 阅读 ( 215 )

1天(1d)和二维阵列(2d array)的区别

...一维数组或一维数组存储相同数据类型的变量列表。可以使用索引访问每个变量。 在Java语言中,int[]数字;声明一个名为number的数组。然后,我们可以使用下面的“new”关键字为该数组分配内存。 数字=新整数[10]; 这个数组可...

  • 发布于 2021-07-01 10:34
  • 阅读 ( 534 )

一维阵列(one-dimensional (1d) array)和二维阵列(two-dimensional (2d) array)的区别

...数组。有一个类似数据类型的变量列表。在一维数组中,使用索引检索元素。如果我们讨论如何将内存分配给一维数组,那么它是通过在代码开头定义数组的大小来分配的。每一种编程语言都有自己的定义数组的方法,如果我们...

  • 发布于 2021-07-08 14:07
  • 阅读 ( 648 )

JAVA(java)和菲律宾比索(php)的区别

...归的缩写。PHP是在PHP许可证下发布的自由软件,由于PHP的使用受到限制,与GNU通用公共许可证(GPL)不兼容。 PHP是一种开源的服务器端HTML嵌入式脚本语言。它基本上可以执行其他CGI程序可以执行的任何任务,但它主要用于创建...

  • 发布于 2021-07-13 05:58
  • 阅读 ( 185 )

矢量(vector)和数组表(arraylist)的区别

...含多种方法。Add()方法用于在向量中添加元素。为此,使用add(index,object)方法。这将在所述索引处添加所述对象。向量是同步的,这意味着在特定时间,只有一个线程能够从外部访问它的方法。因此,向量被认为是线程安...

  • 发布于 2021-07-13 21:02
  • 阅读 ( 177 )
cjfg52207
cjfg52207

0 篇文章

相关推荐