我有一个类需要返回各种类型的多个数据对象,例如ArrayList<Integer>
和double[]
数组。由于java只允许给定方法返回一个对象,因此我尝试将各种数据对象捆绑到 ArrayList 中。但是,有两个问题:
我想出的代码无法读取ArrayList的每个索引中的对象类型。
具体来说,在下面的 ListOfObjects.java 中,Eclipse在行Type mismatch: cannot convert from Object to ArrayList<Integer>
处给出了一条错误消息,指出myAL1=dataHolder.get(0);
,然后是其后三个其他get语句的类似错误消息
我不知道指定哪种类型作为 ArrayList 的数据类型。
我的代码位于以下两个文件中 谁能告诉我如何修复它以便解决这两个问题?
我需要能够随后将 myAL1 用作 ArrayList ,并使用 myDBL1 , mtDBL2 , myDBL3 为 double [] 。
ListOfObjects.java
import java.util.ArrayList;
public class ListOfObjects {
ArrayList<Integer> myAL1 = new ArrayList<Integer>();
double[] myDBL1 = new double[25];
double[] myDBL2 = new double[25];
double[] myDBL3 = new double[25];
public static void main(String[] args) {
}
public void myMethod() {
AssembleListOfObjects myLOO = new AssembleListOfObjects();
ArrayList dataHolder = myLOO.buildListOfObjects();
myAL1 = dataHolder.get(0);
myDBL1 = dataHolder.get(0);
myDBL2 = dataHolder.get(0);
myDBL3 = dataHolder.get(0);
}
}
AssembleListOfObjects.java
import java.util.ArrayList;
public class AssembleListOfObjects {
ArrayList<Integer> al1 = new ArrayList<Integer>();
double[] dbl1 = new double[25];
double[] dbl2 = new double[25];
double[] dbl3 = new double[25];
public void main(String[] args) {
buildListOfObjects();
}
public ArrayList buildListOfObjects() {
ArrayList ListOfObjects = new ArrayList();
ListOfObjects.add(al1);
ListOfObjects.add(dbl1);
ListOfObjects.add(dbl2);
ListOfObjects.add(dbl3);
return ListOfObjects;
}
}
我按照你的方式重新写了,这就是我到目前为止所做的。除非我在代码中的任何位置添加静态修饰符,否则会引发java.lang.NoSuchMethodError: main
错误。
当我在任何地方添加静态修改器时,它会打印出零的数组和一个空的arraylist
您将如何修复此代码,以便 ListOfObjects 能够为每个数组/ arraylist输出值?
但这是代码:
ListOfObjects.java
import java.util.ArrayList;
public class ListOfObjects {
ArrayList<Integer> myAL1 = new ArrayList<Integer>();
double[] myDBL1 = new double[25];
double[] myDBL2 = new double[25];
double[] myDBL3 = new double[25];
public void main(String[] args) {
myMethod();
}
public void myMethod() {
AssembleListOfObjects myLOO = new AssembleListOfObjects();
myAL1 = myLOO.getAl1();
myDBL1 = myLOO.getDbl1();
myDBL2 = myLOO.getDbl2();
myDBL3 = myLOO.getDbl3();
System.out.print("myAL1 is: (");
for (int l = 0; l < myAL1.size(); l++) {
if (l == 0) {
System.out.print(myAL1.get(l));
} else {
System.out.print(", " + myAL1.get(l));
}
}
System.out.println(")");
System.out.print("myDBL1 is: (");
for (int l = 0; l < myDBL1.length; l++) {
if (l == 0) {
System.out.print(myDBL1[l]);
} else {
System.out.print(", " + myDBL1[l]);
}
}
System.out.println(")");
System.out.print("myDBL2 is: (");
for (int l = 0; l < myDBL2.length; l++) {
if (l == 0) {
System.out.print(myDBL2[l]);
} else {
System.out.print(", " + myDBL2[l]);
}
}
System.out.println(")");
System.out.print("myDBL3 is: (");
for (int l = 0; l < myDBL3.length; l++) {
if (l == 0) {
System.out.print(myDBL3[l]);
} else {
System.out.print(", " + myDBL3[l]);
}
}
System.out.println(")");
}
}
AssembleListOfObjects.java
import java.util.ArrayList;
public class AssembleListOfObjects {
private ArrayList<Integer> al1 = new ArrayList<Integer>();
int mySize = 25;
private double[] dbl1 = new double[mySize];
private double[] dbl2 = new double[mySize];
private double[] dbl3 = new double[mySize];
public void main(String[] args) {
setterMethod();
getAl1();
getDbl1();
getDbl2();
getDbl3();
}
public void setterMethod() {
for (int j = 0; j < mySize; j++) {
// the following lines are placeholders for a complex algorithm
dbl1[j] = j;
dbl2[j] = Math.pow((double) j, 3);
dbl3[j] = Math.cos((double) j);
if ((j % 3) == 0) {
al1.add(j);
}
}
}
public ArrayList<Integer> getAl1() {
return al1;
}
public double[] getDbl1() {
return dbl1;
}
public double[] getDbl2() {
return dbl2;
}
public double[] getDbl3() {
return dbl3;
}
}
答案 0 :(得分:5)
尝试返回数组列表中的混合类型并建议您的设计已关闭是一个非常糟糕的设计决策。如果你总是操纵整数的ArrayList和3个双数组,为什么不把它们放在一个类中,这里你调用AssembleListOfObjects,并给该类公共getter或accessor方法获取ArrayList并分别得到3个双数组?然后,如果你需要一个方法来操作这个信息并返回它,它就可以简单地返回这个类的一个对象,而调用该方法的任何人都可以通过调用适当的getter方法来提取所需的信息。
如,
import java.util.ArrayList;
public class AssembleListOfObjects {
private ArrayList<Integer> al1 = new ArrayList<Integer>();
// Also this can be a 2-dimensional array of double
private double[] dbl1 = new double[25];
private double[] dbl2 = new double[25];
private double[] dbl3 = new double[25];
public ArrayList<Integer> getAl1() {
return al1;
}
public double[] getDbl1() {
return dbl1;
}
public double[] getDbl2() {
return dbl2;
}
public double[] getDbl3() {
return dbl3;
}
// public void setter methods
// and any other data manipulation methods
}
查看新编辑的代码,我修改了一些代码,包括向main方法添加一个static修饰符,因此它是一个try main方法,并在一个新的ListOfObjects对象中调用myMethod,因为myMethod不能在静态上下文中调用。必须从适当的对象中调出。我也在myMethod中打电话给myLOO.setterMethod();
:
import java.util.ArrayList;
public class ListOfObjects {
ArrayList<Integer> myAL1 = new ArrayList<Integer>();
double[] myDBL1 = new double[25];
double[] myDBL2 = new double[25];
double[] myDBL3 = new double[25];
// added static to main method
public static void main(String[] args) {
// commented out as this can't be called in a static context, but
// needs to be called on an object
// myMethod();
// created a ListOfObjects object and called myMethod on it
ListOfObjects myListOfObjs = new ListOfObjects();
myListOfObjs.myMethod();
}
public void myMethod() {
AssembleListOfObjects myLOO = new AssembleListOfObjects();
myLOO.setterMethod(); // *** added
myAL1 = myLOO.getAl1();
myDBL1 = myLOO.getDbl1();
myDBL2 = myLOO.getDbl2();
myDBL3 = myLOO.getDbl3();
System.out.print("myAL1 is: (");
for (int l = 0; l < myAL1.size(); l++) {
if (l == 0) {
System.out.print(myAL1.get(l));
} else {
System.out.print(", " + myAL1.get(l));
}
}
System.out.println(")");
System.out.print("myDBL1 is: (");
for (int l = 0; l < myDBL1.length; l++) {
if (l == 0) {
System.out.print(myDBL1[l]);
} else {
System.out.print(", " + myDBL1[l]);
}
}
System.out.println(")");
System.out.print("myDBL2 is: (");
for (int l = 0; l < myDBL2.length; l++) {
if (l == 0) {
System.out.print(myDBL2[l]);
} else {
System.out.print(", " + myDBL2[l]);
}
}
System.out.println(")");
System.out.print("myDBL3 is: (");
for (int l = 0; l < myDBL3.length; l++) {
if (l == 0) {
System.out.print(myDBL3[l]);
} else {
System.out.print(", " + myDBL3[l]);
}
}
;
System.out.println(")");
}
}
class AssembleListOfObjects {
private ArrayList<Integer> al1 = new ArrayList<Integer>();
int mySize = 25;
private double[] dbl1 = new double[mySize];
private double[] dbl2 = new double[mySize];
private double[] dbl3 = new double[mySize];
public void main(String[] args) {
setterMethod();
getAl1();
getDbl1();
getDbl2();
getDbl3();
}
public void setterMethod() {
for (int j = 0; j < mySize; j++) {
// the following lines are placeholders for a complex algorithm
dbl1[j] = j;
dbl2[j] = Math.pow((double) j, 3);
dbl3[j] = Math.cos((double) j);
if ((j % 3) == 0) {
al1.add(j);
}
}
}
public ArrayList<Integer> getAl1() {
return al1;
}
public double[] getDbl1() {
return dbl1;
}
public double[] getDbl2() {
return dbl2;
}
public double[] getDbl3() {
return dbl3;
}
}
答案 1 :(得分:3)
解决编译器错误的方法是使用显式转换,例如:像这样:
ArrayList dataHolder=myLOO.buildListOfObjects();
myAL1=(ArrayList<Integer>)dataHolder.get(0);
myDBL1=(double[])dataHolder.get(0);
然而,正如已经提到的,这是糟糕的设计,你应该把它捆绑在一个类或类似的东西。