Java Array Summary

Java Array Summary


Array Basics

Declaring Array Variables

dataType[] arrayRefVar; //Prefered

or

dataType arrayRefVar[];

Examples:

double[] myList;//Prefered

or

double myList[];

Creating Arrays

arrayRefVar = new dataType[arraySize];

Declaring, Creating, Assigning:

dataType[] arrayRefVar = new dataType[arraySize];

or

dataType arrayRefVar[] = new dataType[arraySize];

Example:

double[] myList = new doublep[10];

Array Size and Default Values

When space for an array is allocated , the array size must be given. When an array is created, its elements are assigned the default value of 0 for the numeric primitive data types, ‘\u0000’ for char types, and false for boolean types.


Array Initializers

dataType[] arrayRefVar = {value0, value1, ... , valuek};

For example,

double[] myList = {1.9, 2.9, 3.4, 3.5};

This statement declares, creates, and initializes the array myList with four elements, which is equivalent to the statements shown below:

double[] myList = new double[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;

Caution:
The new operator is not used in the array initializer syntax.Using an array initializer, you have to declare, create, and initialize the array all in one statement. Splitting it would cause a syntax error. Thus the next statement is wrong:

double[] myList;
myList = {1.9, 2.9, 3.4, 3.5};

Processing Arrays

Cautions:
For an array of the char[] type, it can be printed using one print statement. For example, the following code displays Dallas:

char[] city = {'D', 'a', 'l', 'l', 'a', 's'};
System.out.println(city);

Example: (Finding the smallest index of the largest element)

double max = myList[0];
int indexOfMax = 0;
for (int i = 1; i < myList.length; i++) {
  if (myList[i] > max) {
    max = myList[i];
    indexOfMax = i;
  }
}

foreach Loops

Example:

for (double element: myList) {
  System.out.println(element);
}

Copy Arrays

list2 = list1;

This statement does not copy the contents of the array referenced by list1 to list2, but merely copies the reference value from list1 to list2.
Copy Arrays by reference
In Java, you can use assignment statements to copy primitive data type variables, but not arrays. Assigning one array variable to another array variable actually copies one reference to another and makes both variables point to the same memory location.

There are three ways to copy arrays:

  • Use a loop to copy individual elements one by one.

  • Use the static arraycopy method in the System class.

  • Use the clone method to copy arrays.

Loop:

int[] sourceArray = {2, 3, 1, 5, 10};
int[] targetArray = new int[sourceArray.length];
for (int i = 0; i < sourceArray.length; i++) {
  targetArray[i] = sourceArray[i];
}

arraycopy:

arraycopy(sourceArray, srcPos, targetArray, tarPos, length);

The parameters srcPos and tarPos indicate the starting positions in sourceArray and targetArray, respectively. The number of elements copied from sourceArray to targetArray is indicated by length. For example, you can rewrite the loop using the following statement:

System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);

The arraycopy method does not allocate memory space for the target array. The target array must have already been created with its memory space allocated. After the copying takes place, targetArray and sourceArray have the same content but independent memory locations.

Passing Arrays to Methods

public static void printArray(int[] array) {
  for (int i = 0; i < array.length; i++) {
    System.out.print(array[i] + " ");
  }
}

You can invoke it by passing an array. For example, the following statement invokes the printArray method to display 3, 1, 2, 6, 4, and 2:

printArray(new int[]{3, 1, 2, 6, 4, 2});
new dataType[]{value0, value1, ..., valuek};

There is no explicit reference variable for the array. Such an array is called an anonymous array.

Java uses pass-by-value to pass arguments to a method. There are important differences between passing the values of variables of primitive data types and passing arrays.

For an argument of a primitive type, the argument’s value is passed.

For an argument of an array type, the value of the argument contains a reference to an array; this reference is passed to the method.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值