Lesson 7: The Java Collections Framework and Generics
Activity 27: Read Users from CSV Using Array with Initial Capacity
Solution:
- Create a class called UseInitialCapacity with a main() method
public class UseInitialCapacity {
public static final void main (String [] args) throws Exception {
}
}
- Add a constant field that will be the initial capacity of the array. It will also be used when the array needs to grow:
private static final int INITIAL_CAPACITY = 5;
- Add a static method that will resize arrays. It receives two parameters: an array of Users and an int that represents the new size for the array. It should also return an array of Users. Implement the resize algorithm using System.arraycopy like you did in the previous exercise. Be mindful that the new size might be smaller than the current size of the passed in array:
private static User[] resizeArray(User[] users, int newCapacity) {
User[] newUsers = new User[newCapacity];
int lengthToCopy = newCapacity > users.length...