473,395 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

1
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<String> biggraph = new ArrayList<String>();
//public LinkedList<String>[] 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(filename));
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.println(part[0]);
//System.out.println(part[1]);
}
bufferedReader.close();
}catch (Exception e){
System.err.println("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(String name){
//if biggraph does not contain name, add to big graph, create linked list for it
//Boolean found = false;
Iterator<String> iter = this.biggraph.iterator();
String currItem = "";
while ( iter.hasNext() == true ) {
currItem = iter.next();

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

public void addLink(String from, String into){
//search for from in biggraph, add element to its linked list
Iterator<String> iter = this.biggraph.iterator();
String currItem = "";
int i = 0;
while ( iter.hasNext() == true ) {
currItem = iter.next();
i++;
if (currItem == from) {
/*PROBLEM HERE! LINE BELOW!*/
biggraph[i] = new ArrayList<String>();
}
Dec 24 '10 #1
1 1978
There are other problems in your program apart from the one you have mentioned, e.g.
Expand|Select|Wrap|Line Numbers
  1. graph(part[0], part[1]);
will throw exception, as you have initialized the
Expand|Select|Wrap|Line Numbers
  1. 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
Dec 27 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Novice | last post by:
Hi all, I'm having some trouble with a XPath query I would like to make I have the following simplified XML <A><B><C name="Test"></C><C name="Test2"></C></B><B><C name="Test"></C></B></A I...
4
by: Gustaf Liljegren | last post by:
It doesn't seem to work. I get the error: System.NullReferenceException Object reference not set to an instance of an object. Here's the code: public ArrayList _rar; .... private void...
4
by: rjl | last post by:
Is there a way to assign an arraylist inside an arraylist as a column? i have arrayList list, which has 2 Strings and an arrayList with 3 values. I would like the following columns then in a...
6
by: Lisa | last post by:
I am reading in data from a text file. I want to enter each value on the line into a list and retain the order of the elements. The number of elements and spacing between them varies, but a...
1
by: korkless | last post by:
hi, i have a xml as <itemCollection> <item enabled="false" /> <item enabled="true" /> <item enabled="false" /> </itemCollection> I want know if exists a solution with xml schema for...
1
by: abhighat4214 | last post by:
Hello all. I wanted to ask a simple doubt. I am not so well versed with the syntax and semantics of PHP and I'm new to it. I'm having the following code. $var1="100 <" or $var1 = "> 100" What I...
6
by: Slickuser | last post by:
Hi, I am picking up C#.net and I'm trying to add many values to one single key at different time in a loop. If the key already exist, append new value to previous? I'm not sure how to do that...
1
sid0404
by: sid0404 | last post by:
Hi How do I print elements of an ArrayList of an ArrayList, I think it has to be something similar to a 2 D array, but with a difference, my child arraylist has different number of elements say I...
8
by: srig | last post by:
hi.. i hav created a form which gets empid as input and retrieve data from database and display information about employee.now wen i get empid as input i should check with d table tat empid shold...
0
by: Aravind555 | last post by:
Hi How to get particular value from inside html code of a existing internet explorer page from excel macro (navigate the page by using app activate or any other with out using web query but by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.