如何模仿 Java 中的 ArrayList?

huangapple go评论77阅读模式
英文:

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&lt;E&gt;{
	private Object[] elements;
	public StudentArrayList() {
			elements = new Object[]{null, null};
	}
        public StudentArrayList(int capacity) {
		elements = new Object[capacity];
		for(int i = 0; i &lt; capacity; i++){
			elements[i] = null; 
		}
	}
	public void add(E e){
		Boolean isItAdded = false;
		for ( int i= 0; i &lt; 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 &lt; 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&lt;String&gt; myArrayList = new StudentArrayList&lt;String&gt;(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&lt;E&gt;{
private Object[] elements;
public StudentArrayList() {
elements = new Object[]{null, null};
}
public StudentArrayList(int capacity) {
elements = new Object[capacity];
for(int i = 0; i &lt; capacity; i++){
elements[i] = null; 
}
}
public void add(E e){
Boolean isItAdded = false;
for ( int i= 0; i &lt; 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(&quot;Resize the array &#39;elements&#39; here&quot;);
}
public String toString(){
String stringOfElements=&quot;[&quot;;
Boolean notFirstElement = false;
for ( int i= 0; i &lt; elements.length; i++) {
if(elements[i] != null){
if(notFirstElement) stringOfElements += &quot;, &quot;;
stringOfElements += elements[i];
notFirstElement = true;
} 
} 
stringOfElements += &quot;]&quot;;
return stringOfElements;
}
}
//Main method
package IncreaseArraySize;
public class testMyArrayList {
public static void main(String[] args) {
StudentArrayList&lt;String&gt; myArrayList = new StudentArrayList&lt;String&gt;(2);
System.out.println(&quot;Initial list of elements = &quot; + myArrayList.toString());
myArrayList.add(&quot;this works fine&quot;);
myArrayList.add(&quot;this works fine too&quot;);
System.out.println(&quot;List of elements = &quot; + myArrayList.toString());
myArrayList.add(&quot;This doesn&#39;t work&quot;);
System.out.println(&quot;Array was resized: list of elements= &quot; + 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>

huangapple
  • 本文由 发表于 2023年5月11日 15:03:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定