简体   繁体   English

如何将空的arrayList添加到从类创建的另一个arrayList

[英]How to add an empty arrayList to another arrayList created from a class

I am trying to add an empty arrayList "patternList" to the "treeList" arrayList created from the treeNode class but I am having trouble populating the treeList arrayList with an empty patternList arrayList shown in the last line of the code. 我试图将一个空的arrayList“ patternList”添加到从treeNode类创建的“ treeList” arrayList中,但是我无法用代码最后一行显示的空patternList arrayList填充treeList arrayList。 Basically I want an empty arrayList for each item in the treeList that I will be populating later on. 基本上,我要为treeList中的每个项目提供一个空的arrayList,稍后再填充。 Thanks. 谢谢。

import java.util.*;
import java.util.ArrayList;
import java.util.List;

public class testJava{

    public static class treeNode {
        String nodeName;
        String pNodeName;
        String fNodeName;
        boolean begNode;
        boolean targetNode;
        int nodeNumber;
        //String pattern;
        ArrayList<String> patternList = new ArrayList<String>();
        public treeNode (String nodeName, String pNodeName, boolean begNode, boolean targetNode, int nodeNumber, ArrayList<String> patternList)
        {
            this.nodeName = nodeName;
            this.pNodeName = pNodeName;
            this.begNode = begNode;
            this.targetNode = targetNode;
            this.nodeNumber = nodeNumber;
            this.patternList = patternList;
            //this.pattern = pattern;
        }
    }

    public static void main (String[] args){
        ArrayList<treeNode> treeList = new ArrayList<treeNode>();
        ArrayList<String> openList = new ArrayList<String>();
        ArrayList<String> closeList = new ArrayList<String>();
        ArrayList<String> treePath = new ArrayList<String>();
        String currentNode = "";
        String targetNode = "";
        int smallestNodeCount  = 0;
        int tempNodeCount = 0;
        int openListCount = 0;
        String currentPattern = "" ;

        treeList.add(new treeNode ("S 1", null, true, false, 1, ArrayList<string> patternList));

To create a new empty ArrayList , the syntax would be new ArrayList<String>() . 要创建一个新的空ArrayList ,语法应为new ArrayList<String>()

Since you're immediately passing the newly created list as a parameter to your TreeNode constructor, there's no need for a variable name. 由于您将立即将新创建的列表作为参数传递给TreeNode构造函数,因此不需要变量名。

Your last line should therefore be: 因此,您的最后一行应该是:

treeList.add(new treeNode ("S 1", null, true, false, 1, new ArrayList<String>()));

Also, by convention, Java class names are in UpperCamelCase, not in lowerCamelCase. 同样,按照惯例,Java类名称在UpperCamelCase中,而不在LowerCamelCase中。 That's why it's ArrayList rather than arrayList . 这就是为什么它是ArrayList而不是arrayList

Your class names should be TreeNode and TestJava , with an upper-case T . 您的类名称应为TreeNodeTestJava ,大写T

Another thing to pay attention to is your variable types; 另一件事要注意的是您的变量类型。 more often than not, you want your variable types to use the interface when available, rather than being explicitly tied to a particular implementation class. 通常,您希望变量类型在可用时使用接口,而不是显式地绑定到特定的实现类。 So instead of defining your list using ArrayList<String> openList = new ArrayList<String>(); 因此, ArrayList<String> openList = new ArrayList<String>();使用ArrayList<String> openList = new ArrayList<String>();定义列表, ArrayList<String> openList = new ArrayList<String>();使用ArrayList<String> openList = new ArrayList<String>(); , consider making the type of openList simply List . ,请考虑将openList的类型openListList This only applies on the left-hand side of the declaration. 这仅适用于声明的左侧。 On the right-hand side, you still need the concrete class (ie ArrayList ) when creating the new instance. 在右侧,创建新实例时仍然需要具体的类(即ArrayList )。 Your declaration therefore becomes: 因此,您的声明将变为:

List<String> openList = new ArrayList<>();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM