如何在 Java 中创建可扩展的动态数组而不使用预制类?

发布于 2024-09-02 16:23:41 字数 484 浏览 8 评论 0原文

是的,这是一个家庭作业问题,所以给metehkodezplsthx! :)

无论如何,这就是我需要做的:
我需要一个类,它的属性数组中包含另一个类的对象数组。在我看来,正确的方法是使用 LinkedList、Vector 或类似的东西。不幸的是,上次我这样做时,我得到了教授的火与硫磺,因为根据他的信念,我在不了解基础知识的情况下使用了先进的东西。

现在,下一个明显的解决方案是创建具有固定数量元素的数组,并添加检查以获取和设置,以查看数组是否已满。如果已满,他们将创建新的更大的数组,将旧数组的数据复制到新数组并将新数组返回给调用者。如果它大部分是空的,他们会创建新的较小数组并将数据从旧数组移动到新数组。对我来说这看起来有点愚蠢。对于我的家庭作业,数组中的元素可能不会超过 3 个,但我想制定一个可扩展的解决方案,而无需手动计算有关数组填充频率、添加新元素的平均数量的统计信息,然后使用计算结果来计算新数组中的元素数量等。

顺便说一下,不需要从数组中间删除元素。

有什么建议吗?

Yeah, it's a homework question, so givemetehkodezplsthx! :)

Anyway, here's what I need to do:
I need to have a class which will have among its attributes array of objects of another class. The proper way to do this in my opinion would be to use something like LinkedList, Vector or similar. Unfortunately, last time I did that, I got fire and brimstone from my professor, because according to his belief I was using advanced stuff without understanding basics.

Now next obvious solution would be to create array with fixed number of elements and add checks to get and set which will see if the array is full. If it is full, they'd create new bigger array, copy older array's data to the new array and return the new array to the caller. If it's mostly empty, they'd create new smaller array and move data from old array to new. To me this looks a bit stupid. For my homework, there probably won't be more that 3 elements in an array, but I'd like to make a scalable solution without manually calculating statistics about how often is array filled, what is the average number of new elements added, then using results of calculation to calculate number of elements in new array and so on.

By the way, there is no need to remove elements from the middle of the array.

Any tips?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

金橙橙 2024-09-09 16:23:41
class test {
    private Object[] objects;
    private int size;

    public test() {
        objects = new Object[10];
        size = 0;
    }

    public void push(Object o) {
        if (objects.length == size) {
            throw new RuntimeException("This wouldn't happen if I didn't have to reinvent the wheel");
        }
        objects[size] = o;
        size++;
    }

    public Object pop() {
        size--;
        Object o = objects[size];
        objects[size] = null;
        return o;
    }
}

只是在开玩笑。我认为你最好的选择是实现你自己的链表,然后在你的课堂上使用它。像这样的东西:

class Element {
    Object val;
    Element next;
    Element prev;

    public Element(Object val, Element next, Element prev) {
        this.val = val;
        this.next = next;
        this.prev = prev;
    }

}

class LinkedList {
    Element head;
    Element tail;

    public void add(Object o) {
        Element el = new Element(o, null, tail);
        tail.next = el;
    }

    public Object remove() {
        Element o = tail;
        tail = o.prev;
        tail.next = null;
        return o.val;
    }
}
class test {
    private Object[] objects;
    private int size;

    public test() {
        objects = new Object[10];
        size = 0;
    }

    public void push(Object o) {
        if (objects.length == size) {
            throw new RuntimeException("This wouldn't happen if I didn't have to reinvent the wheel");
        }
        objects[size] = o;
        size++;
    }

    public Object pop() {
        size--;
        Object o = objects[size];
        objects[size] = null;
        return o;
    }
}

Just kidding. I think you're best bet is to implement your own linked list and then use that in your class. Something like:

class Element {
    Object val;
    Element next;
    Element prev;

    public Element(Object val, Element next, Element prev) {
        this.val = val;
        this.next = next;
        this.prev = prev;
    }

}

class LinkedList {
    Element head;
    Element tail;

    public void add(Object o) {
        Element el = new Element(o, null, tail);
        tail.next = el;
    }

    public Object remove() {
        Element o = tail;
        tail = o.prev;
        tail.next = null;
        return o.val;
    }
}
随波逐流 2024-09-09 16:23:41

您想要做的一件事是,当您需要增加数组的大小时,创建一个是旧数组大小两倍的数组。同样,如果需要缩小数组的大小,只需在数组已满一半时才执行此操作。

这将使您不必执行更少的数组复制。

这样做将需要保留一个变量来跟踪数组的实际大小,因为数组的长度不能准确地表示实际大小。

One thing you will want to do is when you need to grow the size of your array create an array that is twice the size of the old array. Likewise, if you need to shrink the size of hte array, only do it once the array is half full.

This will make it so you have to do far less array copies.

Doing this will make it necessary to keep a variable that keeps track of the actual size of the array because the length of the array will not accurately represent the actual size.

執念 2024-09-09 16:23:41

要将现有数组复制到更小或更大的数组中,您可能会找到System#arrayCopy() 有用。

启动示例:

Object[] originalArray = new Object[3];
// ...
Object[] resizedArray = new Object[originalArray.length + 2]; // Grow with 2.
System.arrayCopy(originalArray, 0, resizedArray, 0, originalArray.length);

这会将 originalArray 整个长度上的项目复制到 resizedArray 的开头。 resizedArray 末尾的 2 个插槽仍为 null,以便您可以将其用于其他项目。

这必须让你开始。祝你好运 :)

To copy an existing array into a smaller or larger one, you may find System#arrayCopy() useful.

Kickoff example:

Object[] originalArray = new Object[3];
// ...
Object[] resizedArray = new Object[originalArray.length + 2]; // Grow with 2.
System.arrayCopy(originalArray, 0, resizedArray, 0, originalArray.length);

This will copy the items over the entire length of originalArray into the beginning of resizedArray. The 2 slots at end of resizedArray are still null so that you can use it for other items.

This must get you started. Good luck :)

半山落雨半山空 2024-09-09 16:23:41

是数据结构类吗?听起来你的教授希望你实现自己的 链接列表 数据结构或类似的东西,而不是使用Java 提供的一种。谷歌和你的教科书是你的朋友。

Is it for a data structures class ? Sounds like your professor expects you to implement your own Linked List data structure or something similar instead of using the one Java provides. Google and your text book(s) are your friend.

就像说晚安 2024-09-09 16:23:41

如果我没记错的话,ArrayList 类的工作原理是具有一个固定大小的数组(初始容量为您设置的任何值),该数组在满时以您所描述的方式调整大小。

您可以使用链接列表,尽管听上去您的教授希望您自己编写这些东西,所以创建您自己的类来证明您知道它是如何工作的?

If I recall correctly, the ArrayList class works by having a fixed size array (with an initial capacity of whatever you set) that resizes in pretty much the way you described when it's full.

You could use a linked list, though by the sounds of it your professor wants you to program this stuff by yourself, so create your own class demonstrating you know how it works?

轻拂→两袖风尘 2024-09-09 16:23:41

我认为这真的很简单:p 我们不能在 C 中做到,但可以在 java 中做到

package javaapplication21;

import java.util.Scanner;
public class JavaApplication21 {
    public static void main(String[] args) {
       int a;
       Scanner obj=new Scanner(System.in);
       System.out.print("Enter array size=");
       a=obj.nextInt();
       int b[]=new int[a];
       for(int i=0;i<b.length;i++){
          System.out.println(b[i]+i);
       }
   }
}

i think its really easy way :p which we cant do in C but can do in java

package javaapplication21;

import java.util.Scanner;
public class JavaApplication21 {
    public static void main(String[] args) {
       int a;
       Scanner obj=new Scanner(System.in);
       System.out.print("Enter array size=");
       a=obj.nextInt();
       int b[]=new int[a];
       for(int i=0;i<b.length;i++){
          System.out.println(b[i]+i);
       }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文