java编程思想-----数组

数组

  1. 数组硕果仅存的优点就是效率.
  2. 新生成一个数组对象时,其中所有的引用被自动初始化为null,所以检查其中的引用是否为null,即可知道数组的某个位置是否存有对象.同样基本类型的数组如果是数值型的,就被自动初始化为0,如果时字符型(char)的,就被自动初始化为(char)0;如果是布尔值(boolean),就被自动初始化为false.
  3. Arrays.deepToString()方法,它可以将多维素组转换为多个String.它对基本类型数组和对象数组都起作用.
  4. System.arraycopy()

    • public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)

Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest. The number of components copied is equal to the length argument. The components at positions srcPos through srcPos+length-1 in the source array are copied into positions destPos through destPos+length-1, respectively, of the destination array.
Parameters:
src - the source array.
srcPos - starting position in the source array.
dest - the destination array.
destPos - starting position in the destination data.
length - the number of array elements to be copied.

动态初始化:

  • 数据类型[] 数组名 = new 数据类型[长度];
  • 数据类型:—>决定了数组能够存放什么样的数据
  • [] :—->代表是一个数组
  • 数组名 :—>为数组起的名字,为了找到数组
  • new:—> 向堆内存申请空间的关键字
  • 长度: —>数组中能够存放多少个数据
  • 特点: 我们程序员给定长度,由系统给定默认值!!!

静态初始化:

完整的格式: 数据类型[] 数组名 = new 数据类型[不能写长度!!!!]{元素1,元素2…};
省略的格式:
数据类型[] 数组名 = {元素1,元素2…};
内存划分
*栈
1. 存放局部变量 —-> 定义在方法里面,或者方法声明上(参数列表)的变量都是局部变量
2. 所有的方法都在栈里面运行
3. 堆:
4. 作用:存放new出来的东西
5. 特点:1.都有地址值2.都有默认值整数: 0 小数:0.0 字符:’\u0000 布尔:false 引用数据类型:null

方法区

  • 存放字节码对象.class相关的信息
  • 存放静态相关的东西static
  • 本地方法栈 —->系统相同
  • 寄存器 —>cpu

获取数组的长度
格式:数组名.length

猜你喜欢

转载自blog.csdn.net/zhuofai_/article/details/82352662