数组的打印

 private int[] getArrays(int length) {
        int[] nums = new int[length];
        for (int i = 0; i < length; i++) {
            nums[i] = (int) (Math.random() * 100);
        }

        return nums;
    }

你会怎样打印数组呢?

  int[] nums = this.getArrays(8);
  Log.e("nums", Arrays.toString(nums));

朋友问我为什么这么打印不行,我仅仅想说数组在内存中的存储形式是以对象的方式存储的,对象的地址在栈中,对象的实际内容在堆中,从栈中的引用地址能够找到相应的堆中的实际元素,假设直接打印数组的话仅仅能打印栈中的引用地址而已,想要打印数组内容就必须加上下标,或者能够重写toString方法吧。

正确的打印数组元素:

  int[] nums = this.getArrays(8);
  for (int j = 0; j < nums.length; j++){
      Log.e("nums", nums[j]+"");
  }



原文地址:https://www.cnblogs.com/bhlsheji/p/4244463.html