How do i add an arraylist to a particular value in an arraylist?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jo T
    New Member
    • Dec 2010
    • 1

    How do i add an arraylist to a particular value in an arraylist?

    Hey, I'm trying to create a graph which will later be used to make some sort of pagerank program, but at the moment I'm stuck on the basics.
    "write a class called Graph that encapsulates
    a suitable graph data structure and methods for operating on it (given that the number of nodes
    and edges will potentially be very large). Include methods in your class which load and storegraphs to and from an ASCII text file."

    I have decided to create an array list with all the nodes, then add a link by adding an arraylist to that particular node with the names of the other nodes it links to,i.e 1:2,3
    2:1
    3:4,1
    4:2,3,1
    But i dont know how to add an array list to the node?

    this is my code so far:


    import java.io.*;
    import java.util.*;


    public class pagerank {

    public ArrayList<Strin g> biggraph = new ArrayList<Strin g>();
    //public LinkedList<Stri ng>[] links;
    public static void main(String[] args) {
    new pagerank().read ("pagerank.txt" );
    }


    public void read(String filename) {

    BufferedReader bufferedReader = null;
    try {
    bufferedReader = new BufferedReader( new FileReader(file name));
    String nextline = new String("");
    String currentline = null;
    while ((currentline = bufferedReader. readLine()) != null) {
    nextline = currentline;
    String[] part = new String[1];
    part = nextline.split( " ");
    graph(part[0], part[1]);
    //System.out.prin tln(part[0]);
    //System.out.prin tln(part[1]);
    }
    bufferedReader. close();
    }catch (Exception e){
    System.err.prin tln("Error: " + e.getMessage()) ;

    }
    }

    public void graph(String from, String into){
    addVertex(from) ;
    addVertex(into) ;
    addLink(from, into);
    //checks to see if from exists, if not add
    //checks to see if into exists, if not add
    //checks to see if edge exists within from's linked list, if not add
    }

    public void addVertex(Strin g name){
    //if biggraph does not contain name, add to big graph, create linked list for it
    //Boolean found = false;
    Iterator<String > iter = this.biggraph.i terator();
    String currItem = "";
    while ( iter.hasNext() == true ) {
    currItem = iter.next();

    if (currItem == name) {
    return;
    }
    }
    biggraph.add(na me);
    return;
    }

    public void addLink(String from, String into){
    //search for from in biggraph, add element to its linked list
    Iterator<String > iter = this.biggraph.i terator();
    String currItem = "";
    int i = 0;
    while ( iter.hasNext() == true ) {
    currItem = iter.next();
    i++;
    if (currItem == from) {
    /*PROBLEM HERE! LINE BELOW!*/
    biggraph[i] = new ArrayList<Strin g>();
    }
  • Amit Kumar M
    New Member
    • Nov 2010
    • 9

    #2
    There are other problems in your program apart from the one you have mentioned, e.g.
    Code:
    graph(part[0], part[1]);
    will throw exception, as you have initialized the
    Code:
    String[] part = new String[1];
    for one element in it. There are some compilation errors also (Closing brackets are not completed).
    Hashmap can help you maintaining the node and the link of nodes it connects

    P.S. : Please post the correct code while asking for coding help and use 'CODE' tags.

    thanks,
    Amit

    Comment

    Working...