Four basic characteristics and boundaries of Java arrays-10 days study notes

Four basic characteristics of arrays

  • Its length is determined. Once the array is created, its size cannot be changed .

  • The elements must be of the same type, and mixed types are not allowed.

  • The elements in the array can be any data type, including basic types and reference types.

  • Array variables are reference types, arrays can also be regarded as objects, and each element in the array is equivalent to a member variable of the object.

  • The array itself is an object, and the object in Java is in the heap, so whether the array saves the original type or other object types, the array object itself is in the heap .

Array boundary

  • The legal range of the subscript; [0,length-1], if it crosses the boundary, an error will be reported
 public static void main(String[] args) {
    
    
  //length为5,它的下标为0-4
  int[] nums = new int[5];
  System.out.println(nums[5]);
//如果取5就会报错
//java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
//所以我们不能超过长度
  }

Error java.lang.ArrayIndexOutOfBoundsException

Array summary

  • An array is an ordered collection of the same data type (the data type can be any inner type)
  • Array is also an object, and the array elements are equivalent to the member variables of the object
  • The length of the array is determined, and if it is unchangeable, the following error will be reported:
    ArrayIndexOutOfBoundsException:

Guess you like

Origin blog.csdn.net/yibai_/article/details/114603886