简体   繁体   English

java将对象添加到arrayList

[英]java add object to arrayList

I read most of the other things on SO and I couldn't seem to find an answer. 我在SO上阅读了其他大部分内容,但似乎找不到答案。

  1. Don't know how to write an add method. 不知道如何编写一个add方法。 I'm getting a StackOverflowError. 我收到一个StackOverflowError。 It seems to run infinitely which I am not sure why. 它似乎无限运行,我不确定为什么。
  2. Also, just wanted to confirm that it is possible to write a print function that prints out everything in arraylist myPolygon right? 另外,只是想确认是否可以编写一个打印函数来打印出arraylist myPolygon中的所有内容,对吗?
class IrregularPolygon{
    private ArrayList <Point2D.Double> myPolygon;

   // constructors
   public IrregularPolygon() { }

   // public methods
   public void add(Point2D.Double aPoint) { 
       //System.out.println("in");
       this.add(aPoint);
       System.out.println("finished");

//     for (Point2D.Double number : myPolygon) {
//         System.out.println("Number = " + aPoint);
//     }  
   }
}


public class App{
    public static void main(String[] args){
        IrregularPolygon polygon = new IrregularPolygon();
        Point2D.Double point = new Point2D.Double(1.2, 2.3);
        System.out.println(point);
        polygon.add(point);   

    } // main   
} // class

Calling this.add(aPoint) in add is a recursive call. add调用this.add(aPoint)是一个递归调用。 This method calls itself, and there is no base case, so this results in a StackOverflowError once it recurses deeply enough. 此方法会自我调用,并且没有基本情况,因此一旦深度递归,就会导致StackOverflowError

It looks like you want to add it to the ArrayList , so change 看起来您想将其添加到ArrayList ,所以更改

this.add(aPoint);

to

myPolygon.add(aPoint);

In addition, you never initialized myPolygon , so it's null . 此外,您从未初始化myPolygon ,因此它为null Initialize it: 初始化它:

private ArrayList <Point2D.Double> myPolygon = new ArrayList<>();

Java 方法添加 ArrayList 未定义类型<object><div id="text_translate"><p>我正在为我的作业构建一个 java 程序,我必须将产品添加到特定商店。 尝试从 Store class 添加到 ArrayList 时遇到问题。</p><p> 我有 class 产品如下:</p><pre> class Product { private String pName; private int pPrice; private int pQty; public Product (String pName, int pPrice, int pQty) { this.pName = pName; this.pPrice = pPrice; this.pQty = pQty; } }</pre><p> class 存储如下:</p><pre> class Store { private String storeName; ArrayList&lt;Product&gt; pList =new ArrayList&lt;&gt;(); public Store() { String name = storeName; pList = new ArrayList&lt;Product&gt;(); } public Store(String newStoreName,ArrayList&lt;Product&gt; newPList) { this.storeName = newStoreName; this.pList = newPList; } void setName(String storeName) { this.storeName = storeName; } void setProduct(Product pList) { pList.add(this.pList);//This return method add undefined for type Product, how to solve this error? } String getName() { return storeName; } ArrayList&lt;Product&gt; getProductList() { return pList; } }</pre></div></object> - Java Method add ArrayList is undefined for the type <OBJECT>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将属性添加到 ArrayList<Object> 在 Java 中 - Add properties to ArrayList<Object> in Java Java将对象添加到ArrayList错误 - Java Add Object to ArrayList error 如何在 Java 中将对象添加到 ArrayList - How to add an object to an ArrayList in Java 如何在java中将对象添加到ArrayList中 - How to add an object into ArrayList in java Java 方法添加 ArrayList 未定义类型<object><div id="text_translate"><p>我正在为我的作业构建一个 java 程序,我必须将产品添加到特定商店。 尝试从 Store class 添加到 ArrayList 时遇到问题。</p><p> 我有 class 产品如下:</p><pre> class Product { private String pName; private int pPrice; private int pQty; public Product (String pName, int pPrice, int pQty) { this.pName = pName; this.pPrice = pPrice; this.pQty = pQty; } }</pre><p> class 存储如下:</p><pre> class Store { private String storeName; ArrayList&lt;Product&gt; pList =new ArrayList&lt;&gt;(); public Store() { String name = storeName; pList = new ArrayList&lt;Product&gt;(); } public Store(String newStoreName,ArrayList&lt;Product&gt; newPList) { this.storeName = newStoreName; this.pList = newPList; } void setName(String storeName) { this.storeName = storeName; } void setProduct(Product pList) { pList.add(this.pList);//This return method add undefined for type Product, how to solve this error? } String getName() { return storeName; } ArrayList&lt;Product&gt; getProductList() { return pList; } }</pre></div></object> - Java Method add ArrayList is undefined for the type <OBJECT> Java:如何将String添加到对象Arraylist? - Java: How to add String to an object Arraylist? 读取文本文件并将其添加到Java中的对象的ArrayList中 - Read and Add Text File to an ArrayList of Object in Java Java:扩展ArrayList的add()返回对象 - Java: Extending ArrayList have add() return Object Java ArrayList add class object to list if object name is not already in the list? - Java ArrayList add class object to list if object name is not already in the list? Java ArrayList <Object[]> 反对[][] - Java ArrayList<Object[]> to Object[][]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM