英文:
How to mimic ArrayList in Java?
问题
I have homework to mimic the ArrayList data structure in Java. I am not allowed to use any 3rd party data structures or anything in java.Utils.package.
我有作业要模仿Java中的ArrayList数据结构。不允许使用任何第三方数据结构或java.Utils.package中的内容。
I got stuck on how to increase the array size dynamically.
我陷入了如何动态增加数组大小的困境。
I read the documentation at Class ArrayList<E> and many online articles and I didn't figure out a solution.
我阅读了 Class ArrayList<E> 中的文档以及许多在线文章,但我没有找到解决方案。
I found a post here but the author does not recommend to use his solution. "FAKE resizable array"
我在这里找到了一个帖子,但作者不建议使用他的解决方案。"FAKE resizable array"
This is part of my code:
这是我的代码的一部分:
//Implementation Class
package IncreaseArraySize;
public class StudentArrayList<E>{
private Object[] elements;
public StudentArrayList() {
elements = new Object[]{null, null};
}
public StudentArrayList(int capacity) {
elements = new Object[capacity];
for(int i = 0; i < capacity; i++){
elements[i] = null;
}
}
public void add(E e){
Boolean isItAdded = false;
for ( int i= 0; i < elements.length; i++) {
//assuming the 1st null is the end of the list
if(elements[i] == null){
elements[i] = e;
isItAdded = true;
break;
}
}
if(isItAdded == false){
int holdIndex = elements.length;
resizeArray(holdIndex+10);
elements[holdIndex] = e;
}
}
private void resizeArray(int newCapacity){
System.out.println("Resize the array 'elements' here");
}
public String toString(){
String stringOfElements="[";
Boolean notFirstElement = false;
for ( int i= 0; i < elements.length; i++) {
if(elements[i] != null){
if(notFirstElement) stringOfElements += ", ";
stringOfElements += elements[i];
notFirstElement = true;
}
}
stringOfElements += "]";
return stringOfElements;
}
}
//Main method
package IncreaseArraySize;
public class testMyArrayList {
public static void main(String[] args) {
StudentArrayList<String> myArrayList = new StudentArrayList<String>(2);
System.out.println("Initial list of elements = " + myArrayList.toString());
myArrayList.add("this works fine");
myArrayList.add("this works fine too");
System.out.println("List of elements = " + myArrayList.toString());
myArrayList.add("This doesn't work");
System.out.println("Array was resized: list of elements= " + myArrayList.toString());
}
}
Any help is appreciated.
感谢任何帮助。
英文:
I have homework to mimic the ArrayList data structure in Java. I am not allowed to use any 3rd party data structures or anything in java.Utils.package.
I got stuck on how to increase the array size dynamically.
I read the documentation at Class ArrayList<E> and many online articles and I didn't figure out a solution.
I found a post here but the author does not recommend to use his solution. "FAKE resizable array"
This is part of my code:
//Implementation Class
package IncreaseArraySize;
public class StudentArrayList<E>{
private Object[] elements;
public StudentArrayList() {
elements = new Object[]{null, null};
}
public StudentArrayList(int capacity) {
elements = new Object[capacity];
for(int i = 0; i < capacity; i++){
elements[i] = null;
}
}
public void add(E e){
Boolean isItAdded = false;
for ( int i= 0; i < elements.length; i++) {
//assuming the 1st null is the end of the list
if(elements[i] == null){
elements[i] = e;
isItAdded = true;
break;
}
}
if(isItAdded == false){
int holdIndex = elements.length;
resizeArray(holdIndex+10);
elements[holdIndex] = e;
}
}
private void resizeArray(int newCapacity){
System.out.println("Resize the array 'elements' here");
}
public String toString(){
String stringOfElements="[";
Boolean notFirstElement = false;
for ( int i= 0; i < elements.length; i++) {
if(elements[i] != null){
if(notFirstElement) stringOfElements += ", ";
stringOfElements += elements[i];
notFirstElement = true;
}
}
stringOfElements += "]";
return stringOfElements;
}
}
//Main method
package IncreaseArraySize;
public class testMyArrayList {
public static void main(String[] args) {
StudentArrayList<String> myArrayList = new StudentArrayList<String>(2);
System.out.println("Initial list of elements = " + myArrayList.toString());
myArrayList.add("this works fine");
myArrayList.add("this works fine too");
System.out.println("List of elements = " + myArrayList.toString());
myArrayList.add("This doesn't work");
System.out.println("Array was resized: list of elements= " + myArrayList.toString());
}
}
Any help is appreciated.
答案1
得分: 0
你可以将你的方法实现为
private void resizeArray(int newCapacity) {
Object[] newArray = new Object[newCapacity]; // 创建新的扩展数组
Object[] elements = this.elements; // 获取现有数组
System.arrayCopy(elements, 0, newArray, 0, elements.length); // 将现有数据复制到新数组中
this.elements = newArray; // 将新数组设置为存储
}
英文:
You could implement your method as
private void resizeArray(int newCapacity){
Object[] newArray = new Object[newCapacity]; //create new extended array
Object[] elements = this.elements; //get existing array
System.arrayCopy(elements, 0, newArray, 0, elements.length); //copy existing data into the new array
this.elements = newArray; // set new array as storage
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论