各种类型对象组成的ArrayList

发布于 2024-12-04 10:58:02 字数 5224 浏览 1 评论 0原文

我有一个类需要返回各种类型的多个数据对象,例如 ArrayList和 double[] 数组。由于 java 只允许给定方法返回一个对象,因此我尝试将各种数据对象捆绑到一个 ArrayList 中。但是,存在两个问题:

  1. 我提出的代码无法读取 ArrayList 每个索引中的对象类型。
    具体来说,在下面的 ListOfObjects.java 中,Eclipse 在 myAL1=dataHolder 行给出一条错误消息,指出 Typemismatch: Cannot Convert from Object to ArrayList .get(0);,后面跟着它后面的其他三个 get 语句的类似错误消息。

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

我的代码位于下面的两个文件中。
谁能告诉我如何修复它,以便解决这两个问题?

我随后需要能够将 myAL1 用作 ArrayList,并使用 myDBL1mtDBL2 和 < strong>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;
    }
}

编辑:

我按照你的方式重新编写了它,这是我到目前为止所拥有的。除非我在代码中的任何位置添加 static 修饰符,否则它会抛出 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;
    }
}

I have a class that needs to return multiple data objects of various types, such as an ArrayList<Integer> and arrays of double[]. Since java only allows one object to be returned by a given method, I am trying to bundle the various data objects into an ArrayList. However, there are two problems:

  1. The code I am coming up with is unable to read the type of object in each index of the ArrayList.
    Specifically, in ListOfObjects.java below, Eclipse gives me an error message stating Type mismatch: cannot convert from Object to ArrayList<Integer> at the line myAL1=dataHolder.get(0);, followed by similar error messages for the three other get statements that follow it.

  2. I do not know what type to specify as the data type for the ArrayList.

My code is in two files below.
Can anyone show me how to fix it so that these two problems are fixed?

I need to be able to subsequently use myAL1 as an ArrayList, and to use myDBL1, mtDBL2, and myDBL3 as 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;
    }
}

EDIT:

I re-wrote it your way, and here is what I have so far. It throws a java.lang.NoSuchMethodError: main error unless I add the static modifier everywhere in the code.
When I do add the static modifier everywhere, it prints out arrays of zeros and an empty arraylist.
How would you fix this code so that ListOfObjects is able to output values for each of the arrays/arraylist?

But here is the code:

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

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

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

发布评论

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

评论(2

我很坚强 2024-12-11 10:58:02

尝试在数组列表中返回混合类型是一个非常糟糕的设计决策,并且表明您的设计已关闭。如果您总是操作 Integer 的 ArrayList 和 3 个双精度数组,为什么不将它们放在一个类中,在这里调用 AssembleListOfObjects,并为该类提供公共 getter 或访问器方法来获取 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;
   }
}

It's a very bad design decision to try to return mixed types in an array list and suggests that your design is off. If you're always manipulating the ArrayList of Integer and 3 double arrays, why not put them in a class, here you call AssembleListOfObjects, and give that class public getter or accessor methods to get the ArrayList and to get the 3 double arrays individually? Then if you need a method to manipulate this information and return it, it can simply return an object of this class, and whoever calls the method can extract the information it needs by calling the appropriate getter method.

e.g.,

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

}

Looking at your newly edited code, I modified it some including adding a static modifier to your main method so it is a try main method, and calling myMethod inside on a new ListOfObjects object since myMethod cannot be called in a static context but must be called off of an appropriate object. I've also a call to myLOO.setterMethod(); from within myMethod:

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;
   }
}
梦回梦里 2024-12-11 10:58:02

解决编译器错误的方法是使用显式强制转换,例如这样:

ArrayList dataHolder=myLOO.buildListOfObjects();
myAL1=(ArrayList<Integer>)dataHolder.get(0);
myDBL1=(double[])dataHolder.get(0);

但是,正如已经提到的,这是一个糟糕的设计,您可能应该将其捆绑在一个类或类似的东西中。

The way to get around the compiler errors is to use explicit casts, e.g. like this:

ArrayList dataHolder=myLOO.buildListOfObjects();
myAL1=(ArrayList<Integer>)dataHolder.get(0);
myDBL1=(double[])dataHolder.get(0);

However, as already mentioned, this is bad design and you should probably bundle it in a class or something like this.

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