将对象添加到 ArrayList 时遇到问题

发布于 2024-08-14 11:30:22 字数 504 浏览 11 评论 0原文

我的问题是关于如何将对象添加到数组中,在我的例子中,我有一个包含 4 列的数组类,但我无法让程序将对象添加到数组中。

public class DatabaseTable extends AbstractTableModel { 

    public ArrayList<Object> objects = new ArrayList<Object>(); 

    public void add(Object o, String sort, String getDesc) { 
        objects.add(o); 
    } 
    // ... 
}

我尝试过:

DatabaseTable dt = new DatabaseTable();
dt.add("something", "something", "something", "something");

但我的程序无法运行..有人知道怎么做吗?

My question is about how I add an object to an array, in my case I have an array class with 4 columns and I cant get my program to add an object to the array.

public class DatabaseTable extends AbstractTableModel { 

    public ArrayList<Object> objects = new ArrayList<Object>(); 

    public void add(Object o, String sort, String getDesc) { 
        objects.add(o); 
    } 
    // ... 
}

I have tried with:

DatabaseTable dt = new DatabaseTable();
dt.add("something", "something", "something", "something");

but my program wont run.. Anyone how knows how to do it?

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

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

发布评论

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

评论(8

你げ笑在眉眼 2024-08-21 11:30:22

看起来您正在将 4 个参数传递给 3 个参数的函数。

It looks like you're passing 4 arguments to a 3 argument function.

强辩 2024-08-21 11:30:22

通过添加,您的意思是为数组元素分配一个值吗?

yourArray[0] = yourObject1;
yourArray[1] = yourObject2;
//...

By add, do you mean assign a value to an array element?

yourArray[0] = yourObject1;
yourArray[1] = yourObject2;
//...
ヤ经典坏疍 2024-08-21 11:30:22

我不知道 DatabaseTable 类。你尝试过ArrayList吗?
像这样的东西:

ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
ArrayList<String> tmp = new ArrayList<String>();
tmp.add("something");
tmp.add("foo");
tmp.add("bar");
tmp.add("wtf");
list.add(tmp);

I don't know the class DatabaseTable. Have you tried with ArrayList?
Something like:

ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
ArrayList<String> tmp = new ArrayList<String>();
tmp.add("something");
tmp.add("foo");
tmp.add("bar");
tmp.add("wtf");
list.add(tmp);
傻比既视感 2024-08-21 11:30:22

DatabaseTable 是您开发的用于实现数组行为的类吗?如果是这种情况,您需要调试 add() 实现。如果没有上下文,我们就无法提供更多帮助。

如果您由于家庭作业而自己实现数组,首先您必须了解该语言提供的数组结构的存在和行为。为什么你的程序不运行?它抛出异常?

查看Java 教程如何介绍数组。还应检查 Collections 框架。

Is DatabaseTable some class you developed to implement an array behaviour? If that's the case, you need to debug the add() implementation. Without context we can't help more.

If you are implementing an array by yourself due to a homework assigment, first you must be aware of the existence and behaviour of the array structure provided by the language. Why your program don't run? It throws an exception?

Check how arrays are covered by The Java Tutorial. The Collections framework also should be checked.

陪你搞怪i 2024-08-21 11:30:22

取决于它有什么支持。例如,如果是 java.util. ArrayList,那么你可以使用它的 add() 方法:

List<String> list = new ArrayList<String>();
list.add("something");
list.add("something");
list.add("something");
list.add("something");

如果您想在 DatabaseTable#add() 方法中使用可变参数,那么它应该看起来像:

public void add(String... strings) {
    list.addAll(Arrays.asList(strings));
}

或者更高效一点:

public void add(String... strings) {
    for (String string : strings) {
        list.add(strings);
    }
}

这样你就可以按照你想要的方式使用它:

dt.add("something", "something", "something", "something");

但是如果它是由一个普通的 String[] 数组支持,那么你需要做一些更多工作:

private String[] array = new String[0];

public void add(String... strings) {
    int length = array.length;
    String[] newArray = new String[length + strings.length];
    System.arraycopy(array, 0, newArray, 0, length);
    array = newArray;
    for (int i = 0; i < strings.length; i++) {
        array[length + i] = strings[i];
    }
}

有关集合(其中 ArrayList 所属)和数组的更多信息,您可能会发现 Sun 教程很有用:试用:集合语言基础知识:数组

Depends on what it is backed by. If it is for instance java.util.ArrayList, then you can just use its add() method:

List<String> list = new ArrayList<String>();
list.add("something");
list.add("something");
list.add("something");
list.add("something");

If you want to use varargs in your DatabaseTable#add() method, then it should look like:

public void add(String... strings) {
    list.addAll(Arrays.asList(strings));
}

or a bit more efficient:

public void add(String... strings) {
    for (String string : strings) {
        list.add(strings);
    }
}

this way you can use it as you wanted:

dt.add("something", "something", "something", "something");

But if it is instead backed by a plain String[] array, then you'll need to do a bit more work:

private String[] array = new String[0];

public void add(String... strings) {
    int length = array.length;
    String[] newArray = new String[length + strings.length];
    System.arraycopy(array, 0, newArray, 0, length);
    array = newArray;
    for (int i = 0; i < strings.length; i++) {
        array[length + i] = strings[i];
    }
}

For more about collections (where ArrayList fall in) and arrays you may find the Sun tutorials useful: Trial: Collections and Language basics: Arrays.

晒暮凉 2024-08-21 11:30:22

你可以使用这样的东西:

public Object[] addObjectToArray(Object[] array, Object o)
{
    Object[] ret = new Object[array.length + 1];
    System.arraycopyt(array, 0, ret, 0, array.length);
    ret[array.length] = o;
    return ret;
}

You can use something like this:

public Object[] addObjectToArray(Object[] array, Object o)
{
    Object[] ret = new Object[array.length + 1];
    System.arraycopyt(array, 0, ret, 0, array.length);
    ret[array.length] = o;
    return ret;
}
毁梦 2024-08-21 11:30:22

在您的pastebay 代码中,DatabaseTable.add() 有三个参数,而上面的代码有四个。

In your pastebay code, DatabaseTable.add() has three arguments, and your code above has four.

救赎№ 2024-08-21 11:30:22

您可以通过创建特定长度的数组来创建对象数组,并且可以按位置将对象放入数组中。

public static Object [] addToArray(){
    List list1 = new ArrayList();
    list1.add("11111");

    List list2 = new ArrayList();
    list2.add("2222222222");

    List list3 = new ArrayList();
    list3.add("3333333333");

    List list4 = new ArrayList();
    list4.add("444444444");
    Object []arr = new Object[4];

    arr[0] = list1;
    arr[1] = list2;
    arr[2] = list3;
    arr[3] = list4;
    return arr;

}

You can create array of objects by creating array of particular length and you can put objects on array by position.

public static Object [] addToArray(){
    List list1 = new ArrayList();
    list1.add("11111");

    List list2 = new ArrayList();
    list2.add("2222222222");

    List list3 = new ArrayList();
    list3.add("3333333333");

    List list4 = new ArrayList();
    list4.add("444444444");
    Object []arr = new Object[4];

    arr[0] = list1;
    arr[1] = list2;
    arr[2] = list3;
    arr[3] = list4;
    return arr;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文