将对象添加到 ArrayList 时遇到问题
我的问题是关于如何将对象添加到数组中,在我的例子中,我有一个包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
看起来您正在将 4 个参数传递给 3 个参数的函数。
It looks like you're passing 4 arguments to a 3 argument function.
通过添加,您的意思是为数组元素分配一个值吗?
By add, do you mean assign a value to an array element?
我不知道 DatabaseTable 类。你尝试过ArrayList吗?
像这样的东西:
I don't know the class DatabaseTable. Have you tried with ArrayList?
Something like:
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.
取决于它有什么支持。例如,如果是
java.util. ArrayList
,那么你可以使用它的add()
方法:如果您想在
DatabaseTable#add()
方法中使用可变参数,那么它应该看起来像:或者更高效一点:
这样你就可以按照你想要的方式使用它:
但是如果它是由一个普通的
String[]
数组支持,那么你需要做一些更多工作:有关集合(其中
ArrayList
所属)和数组的更多信息,您可能会发现 Sun 教程很有用:试用:集合 和 语言基础知识:数组。Depends on what it is backed by. If it is for instance
java.util.ArrayList
, then you can just use itsadd()
method:If you want to use varargs in your
DatabaseTable#add()
method, then it should look like:or a bit more efficient:
this way you can use it as you wanted:
But if it is instead backed by a plain
String[]
array, then you'll need to do a bit more work:For more about collections (where
ArrayList
fall in) and arrays you may find the Sun tutorials useful: Trial: Collections and Language basics: Arrays.你可以使用这样的东西:
You can use something like this:
在您的pastebay 代码中,DatabaseTable.add() 有三个参数,而上面的代码有四个。
In your pastebay code, DatabaseTable.add() has three arguments, and your code above has four.
您可以通过创建特定长度的数组来创建对象数组,并且可以按位置将对象放入数组中。
You can create array of objects by creating array of particular length and you can put objects on array by position.