简体   繁体   中英

How to assign values to an ArrayList in a class?

I have been having trouble for the longest time on figuring out why you seemingly cannot assign values to an ArrayList without getting an error.

The first block of code is the main method which gets line by line from a textfile and splits the line using a delimiter. The first string is stored in one variable as a long, then other 9 strings are stored in an ArrayList. It does this for every line in the file.

I have debugged this code many times and it shows that the array is getting the right values.

The issues is when the code reaches the part where it calls insert, which I have commented in the code.

It first goes to create the node, but when it reaches the part when it is going to add the values from the first ArrayList to the newly created ArrayList, everything breaks. The for loop stops functioning as it should and it continues to increment even though the limit was reached.

I have decided to omit the BinaryTree Class which I also use for this project because it works as it should.

So how would I go about properly assigning the values from the ArrayList that I pass to the Node ArrayList?

package assignment7;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.Long.parseLong;
import java.util.Scanner;
import java.util.ArrayList;

public class Assignment7 {

    public static void main(String[] args) throws IOException {

    boolean headerLine = true;
    String firstLine = "";
    String catchLine;
    String token;
    long s_cid;
    ArrayList<String> Arr = new ArrayList<String>();
    Scanner delimS;
    int count = 0;
    BinaryTree snomedTree = new BinaryTree();

    try(BufferedReader br = new BufferedReader(new FileReader("Data.txt"))) {

        while ((catchLine = br.readLine()) != null) {

            for(int i = 0; i < 9; i++){

                Arr.add("");

            }

            if(headerLine){

                firstLine = catchLine;
                headerLine = false;

            }

            else{

                delimS = new Scanner(catchLine);
                delimS.useDelimiter("\\|");
                s_cid = parseLong(delimS.next());

                while(delimS.hasNext()){

                    token = delimS.next();
                    Arr.set(count, token);
                    count++;

                }

//Runs fine up to here then this insert function is called

                  snomedTree.insert(new Node(s_cid, Arr));

            }

            Arr.clear();

        }
    }

    try (PrintWriter writer = new PrintWriter("NewData.txt")) {

        writer.printf(firstLine);
        snomedTree.inorder(snomedTree.root, writer);

    }
}

}

Here is the Node class:

package assignment7;

import java.util.ArrayList;



class Node {

public long cid;
public ArrayList<String> Satellite;
public Node l;
public Node r;

public Node(long cid, ArrayList<String> Sat) {
    this.Satellite = new ArrayList<String>();
    this.cid = cid;

//This for loop continues to run even after i = 9 for(int i = 0; 0 < 9; i++){

        Satellite.add(Sat.get(i));

    }

}

}

And this is the Binary Tree class:

package assignment7;
import java.util.ArrayList;
import java.io.PrintWriter;


public class BinaryTree {
public Node root;
public BinaryTree() {
    this.root = null;
}
public void insert(Node x) {
    if (root == null) {
        root = x;
    } 

    else {
        Node current = root;
        while (true) {

            if (x.cid > current.cid) {
                if (current.r == null) {
                    current.r = x;
                    break;
                } 

                current = current.r;
            }

            else {
                if (current.l == null) {
                    current.l = x;
                    break;
                } 
                current = current.l;
            }
        }

    }
}

public void inorder(Node x, PrintWriter out) {

    if (x != null) {

    inorder(x.l, out);
        out.printf(String.valueOf(x.cid) + "|");
        for(int i = 0; i < 9; i++){

            if(i != 8){

                out.printf(x.Satellite.get(i) + "|");

            }

            else{

                out.printf(x.Satellite.get(i) + "\n");

            }
        }
        inorder(x.r, out);
    }


}
}
public Node(long cid, ArrayList<String> Sat) {
    this.Satellite = new ArrayList<String>();
    this.cid = cid;
    for(int i = 0; i < Sat.size()-1; i++){

        Satellite.add(Sat.get(i));

    }

}

here 0<9 is wrong, what error are u getting ??

Thanks to Miller Cy Chan's comment it works perfect now.

I changed the Node class to:

package assignment7;

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


class Node {

    public long cid;
    public ArrayList<String> Satellite;
    public Node l;
    public Node r;

    public Node(long cid, ArrayList<String> Sat) {

    this.Satellite = new ArrayList<String>(Sat.subList(0, Math.min(9, Sat.size())));
    this.cid = cid;


    }

}

I also made sure to change the "count" variable in the main back to 0 after each iteration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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