gpt4 book ai didi

java - 由各种类型的对象组成的ArrayList

转载 作者:行者123 更新时间:2023-12-03 18:06:49 25 4
gpt4 key购买 nike

我有一个类需要返回多个不同类型的数据对象,比如 ArrayList<Integer>double[] 的数组.由于 java 只允许给定方法返回一个对象,因此我试图将各种数据对象捆绑到一个 ArrayList 中。但是,有两个问题:

  1. 我编写的代码无法读取 ArrayList 的每个索引中的对象类型。
    具体来说,在下面的 ListOfObjects.java 中,Eclipse 给我一条错误消息,指出 Type mismatch: cannot convert from Object to ArrayList<Integer>myAL1=dataHolder.get(0); 行, 随后是其他三个 get 语句的类似错误消息。

  2. 我不知道要为ArrayList 指定什么类型作为数据类型。

我的代码在下面的两个文件中。
任何人都可以告诉我如何解决它以便解决这两个问题吗?

我需要能够随后将 myAL1 用作 ArrayList,并使用 myDBL1mtDBL2myDBL3 作为 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错误,除非我在代码中的任何地方添加 static 修饰符。
当我在所有地方添加静态修饰符时,它会打印出零数组和一个空数组列表。
您将如何修复此代码,以便 ListOfObjects 能够为每个数组/数组列表输出值?

但是这里是代码:

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;
}
}

最佳答案

尝试在数组列表中返回混合类型是一个非常糟糕的设计决定,并表明您的设计已关闭。如果您总是操作 Integer 的 ArrayList 和 3 个 double 组,为什么不将它们放在一个类中,在这里您调用 AssembleListOfObjects,并为该类提供公共(public) getter 或访问器方法以获取 ArrayList 并分别获取 3 个 double 组?那么如果你需要一个方法来操作这个信息并返回它,它可以简单地返回一个这个类的对象,谁调用这个方法就可以通过调用适当的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 方法添加一个静态修饰符,使其成为一个 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;
}
}

关于java - 由各种类型的对象组成的ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7409783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com