如何模仿 Java 中的 ArrayList?

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

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:

这是我的代码的一部分:

  1. //Implementation Class
  2. package IncreaseArraySize;
  3. public class StudentArrayList&lt;E&gt;{
  4. private Object[] elements;
  5. public StudentArrayList() {
  6. elements = new Object[]{null, null};
  7. }
  8. public StudentArrayList(int capacity) {
  9. elements = new Object[capacity];
  10. for(int i = 0; i &lt; capacity; i++){
  11. elements[i] = null;
  12. }
  13. }
  14. public void add(E e){
  15. Boolean isItAdded = false;
  16. for ( int i= 0; i &lt; elements.length; i++) {
  17. //assuming the 1st null is the end of the list
  18. if(elements[i] == null){
  19. elements[i] = e;
  20. isItAdded = true;
  21. break;
  22. }
  23. }
  24. if(isItAdded == false){
  25. int holdIndex = elements.length;
  26. resizeArray(holdIndex+10);
  27. elements[holdIndex] = e;
  28. }
  29. }
  30. private void resizeArray(int newCapacity){
  31. System.out.println("Resize the array 'elements' here");
  32. }
  33. public String toString(){
  34. String stringOfElements="[";
  35. Boolean notFirstElement = false;
  36. for ( int i= 0; i &lt; elements.length; i++) {
  37. if(elements[i] != null){
  38. if(notFirstElement) stringOfElements += ", ";
  39. stringOfElements += elements[i];
  40. notFirstElement = true;
  41. }
  42. }
  43. stringOfElements += "]";
  44. return stringOfElements;
  45. }
  46. }
  47. //Main method
  48. package IncreaseArraySize;
  49. public class testMyArrayList {
  50. public static void main(String[] args) {
  51. StudentArrayList&lt;String&gt; myArrayList = new StudentArrayList&lt;String&gt;(2);
  52. System.out.println("Initial list of elements = " + myArrayList.toString());
  53. myArrayList.add("this works fine");
  54. myArrayList.add("this works fine too");
  55. System.out.println("List of elements = " + myArrayList.toString());
  56. myArrayList.add("This doesn't work");
  57. System.out.println("Array was resized: list of elements= " + myArrayList.toString());
  58. }
  59. }

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:

  1. //Implementation Class
  2. package IncreaseArraySize;
  3. public class StudentArrayList&lt;E&gt;{
  4. private Object[] elements;
  5. public StudentArrayList() {
  6. elements = new Object[]{null, null};
  7. }
  8. public StudentArrayList(int capacity) {
  9. elements = new Object[capacity];
  10. for(int i = 0; i &lt; capacity; i++){
  11. elements[i] = null;
  12. }
  13. }
  14. public void add(E e){
  15. Boolean isItAdded = false;
  16. for ( int i= 0; i &lt; elements.length; i++) {
  17. //assuming the 1st null is the end of the list
  18. if(elements[i] == null){
  19. elements[i] = e;
  20. isItAdded = true;
  21. break;
  22. }
  23. }
  24. if(isItAdded == false){
  25. int holdIndex = elements.length;
  26. resizeArray(holdIndex+10);
  27. elements[holdIndex] = e;
  28. }
  29. }
  30. private void resizeArray(int newCapacity){
  31. System.out.println(&quot;Resize the array &#39;elements&#39; here&quot;);
  32. }
  33. public String toString(){
  34. String stringOfElements=&quot;[&quot;;
  35. Boolean notFirstElement = false;
  36. for ( int i= 0; i &lt; elements.length; i++) {
  37. if(elements[i] != null){
  38. if(notFirstElement) stringOfElements += &quot;, &quot;;
  39. stringOfElements += elements[i];
  40. notFirstElement = true;
  41. }
  42. }
  43. stringOfElements += &quot;]&quot;;
  44. return stringOfElements;
  45. }
  46. }
  47. //Main method
  48. package IncreaseArraySize;
  49. public class testMyArrayList {
  50. public static void main(String[] args) {
  51. StudentArrayList&lt;String&gt; myArrayList = new StudentArrayList&lt;String&gt;(2);
  52. System.out.println(&quot;Initial list of elements = &quot; + myArrayList.toString());
  53. myArrayList.add(&quot;this works fine&quot;);
  54. myArrayList.add(&quot;this works fine too&quot;);
  55. System.out.println(&quot;List of elements = &quot; + myArrayList.toString());
  56. myArrayList.add(&quot;This doesn&#39;t work&quot;);
  57. System.out.println(&quot;Array was resized: list of elements= &quot; + myArrayList.toString());
  58. }
  59. }

Any help is appreciated.

答案1

得分: 0

你可以将你的方法实现为

  1. private void resizeArray(int newCapacity) {
  2. Object[] newArray = new Object[newCapacity]; // 创建新的扩展数组
  3. Object[] elements = this.elements; // 获取现有数组
  4. System.arrayCopy(elements, 0, newArray, 0, elements.length); // 将现有数据复制到新数组中
  5. this.elements = newArray; // 将新数组设置为存储
  6. }
英文:

You could implement your method as

  1. private void resizeArray(int newCapacity){
  2. Object[] newArray = new Object[newCapacity]; //create new extended array
  3. Object[] elements = this.elements; //get existing array
  4. System.arrayCopy(elements, 0, newArray, 0, elements.length); //copy existing data into the new array
  5. this.elements = newArray; // set new array as storage
  6. }
  7. </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:

确定