Docsity

Resizing Arrays in Java: Creating and Copying Elements, Study notes of Computer Science

Partial preview of the text

Download Resizing Arrays in Java: Creating and Copying Elements and more Study notes Computer Science in PDF only on Docsity!

RESIZING ARRAYS

CMSC 150: Introduction to Computing

 int[] intArray = new int[4];  for (int i = 0; i < 4; i++) { intArray[i] = i; } 127 128 129 130 131 132

 int[] intArray = new int[4];  for (int i = 0; i < 4; i++) { intArray[i] = i; } 127 128 129 130 131 132 intArray  int[] intArray = new int[4];  for (int i = 0; i < 4; i++) { intArray[i] = i; } 127 128 129 130 131 132 intArray

 int[] intArray = new int[4];  for (int i = 0; i < 4; i++) { intArray[i] = i; } 127 128 129 130 131 132 intArray 129 i (^0) (^0) • intArray[i] is a memory address

  • Java treats it as intArray + i = 129 + 0 = 129
  • So when i=0, intArray[i] is memory address 129  int[] intArray = new int[4];  for (int i = 0; i < 4; i++) { intArray[i] = i; } 127 128 129 130 131 132 intArray 129 i (^1) 0 1
  • intArray[i] is a memory address
  • Java treats it as intArray + i = 129 + 1 = 130
  • So when i=1, intArray[i] is memory address 130

 int[] intArray = new int[4];  for (int i = 0; i < 4; i++) { intArray[i] = i; } 127 128 129 130 131 132 intArray 129 i (^2) 0 1 2  int[] intArray = new int[4];  for (int i = 0; i < 4; i++) { intArray[i] = i; } 127 128 129 130 131 132 intArray 129 i (^3) 0 1 2 3

int[] newArray = new int[5]; for (int i = 0; i < 4; i++) { newArray[i] = intArray[i]; } intArray = newArray; 127 128 129 130 131 132 intArray 129 0 1 2 3 158 159 160 161 162 163 newArray int[] newArray = new int[5]; for (int i = 0; i < 4; i++) { newArray[i] = intArray[i]; } intArray = newArray; 127 128 129 130 131 132 intArray 129 0 1 2 3 158 159 160 161 162 163 newArray

int[] newArray = new int[5]; for (int i = 0; i < 4; i++) { newArray[i] = intArray[i]; } intArray = newArray; 127 128 129 130 131 132 intArray 129 0 1 2 3 158 159 160 161 162 163 newArray 158 int[] newArray = new int[5]; for (int i = 0; i < 4; i++) { newArray[i] = intArray[i]; } intArray = newArray; 127 128 129 130 131 132 intArray 129 0 1 2 3 158 159 160 161 162 163 newArray 158 i 0

int[] newArray = new int[5]; for (int i = 0; i < 4; i++) { newArray[i] = intArray[i]; } intArray = newArray; 127 128 129 130 131 132 intArray 129 0 1 2 3 158 159 160 161 162 163 newArray 158 i 2

int[] newArray = new int[5]; for (int i = 0; i < 4; i++) { newArray[i] = intArray[i]; } intArray = newArray; 127 128 129 130 131 132 intArray 129 0 1 2 3 158 159 160 161 162 163 newArray 158 i 3

int[] newArray = new int[5]; for (int i = 0; i < 4; i++) { newArray[i] = intArray[i]; } intArray = newArray; 127 128 129 130 131 132 intArray 129 0 1 2 3 158 159 160 161 162 163 newArray 158

int[] newArray = new int[5]; for (int i = 0; i < 4; i++) { newArray[i] = intArray[i]; } intArray = newArray; 127 128 129 130 131 132 intArray 158 158 159 160 161 162 163 newArray 158

Integer[] objArray = new Integer[2]; for (int i = 0; i < 2; i++) { objArray[i] = new Integer(i); } 127 128 129 130 131 132 objArray 129 158 159 160 161 162 163 int value: 0 intValue() toString() … int value: 1 intValue() toString() …