|
-
January 19th, 2012, 11:12 PM
#1
link Arraylist in java
i have two arraylist. i have to link the one arraylist with another arraylist's perticular index. can you help me?
-
January 20th, 2012, 01:36 AM
#2
Re: link Arraylist in java
 Originally Posted by sugir1987
i have two arraylist. i have to link the one arraylist with another arraylist's perticular index. can you help me?
It's not clear, at least not to me, exactly how you want the ArrayLists to be "linked"?
But you can always create a new ArrayList and add on the elements in the order you want them.
Last edited by nuzzle; January 20th, 2012 at 02:58 AM.
-
January 20th, 2012, 10:08 AM
#3
Re: link Arraylist in java
i have to create tree. the parent has more than two children. children are stored into arraylist now we have to point the parent node that is another arraylist
-
January 20th, 2012, 11:59 AM
#4
Re: link Arraylist in java
Rather than trying to link the ArrayLists together you should create a Node class that contains the nodes data and an Arraylist<Node> which contains the child Node's.
If you want to have bidirectional linking then also have a field of type Node which links back to the parent Node.
-
January 20th, 2012, 11:34 PM
#5
Re: link Arraylist in java
i cantn't get. can you post the code?
-
January 21st, 2012, 05:23 AM
#6
Re: link Arraylist in java
The following is a bare bones representation of what I was suggesting.
Code:
class Node<E> {
E data;
List<Node<E>> children = new ArrayList<Node<E>>();
}
-
January 21st, 2012, 08:08 AM
#7
Re: link Arraylist in java
Are you trying to draw Tree in Swing ?
-
January 21st, 2012, 08:28 PM
#8
Re: link Arraylist in java
i'm a beginner of java. you have used Node<E>.can you explain about <E>? now i did not use this(<>) type of class. now i have to create dynamic children.a single array-list is possible to do this?
for example i have to create this kind of tree
7
/ | \
2 7 12
/ | \ / \ / \
1 2 3 6 8 11 13
here each parent has average of children. here i have three condition such as (consider the child node as x) 1<=x<=5,6<=x<=10,x>=11.here i have three group of children.how can separate this group of children from a single array-list. is it possible or not. if it is possible can you give me a code to insert the children in array-list (with example).
-
January 22nd, 2012, 01:45 AM
#9
Re: link Arraylist in java
here <> is for representing Generics in java. Generics provide type-safety by checking type of Class at compile time to avoid ClassCastException in runtime e.g.
ArrayList<String> means ArrayList can only hold String object and no other object , if you try to insert Integer into ArrayList it will throw compilation error.
Node<E> means creating a generic class which can hold any type of node e.g. Node<String> or Node<Integer>. you can learn more about Generic on my post http://javarevisited.blogspot.com/20...-tutorial.html
-
January 22nd, 2012, 09:08 AM
#10
Re: link Arraylist in java
-
January 23rd, 2012, 08:40 PM
#11
tree code error in java
can you help me to clear the error?
import java.util.*;
class Node{
protected Object data;
protected static ArrayList child;
protected Node next;
public Node(){
next = null;
data = null;
child=new ArrayList();
}
public Node(Object d,Node n){
data = d;
next = n;
n.child=new ArrayList();
}
}
public class Tree{
protected Node head;
public Tree(){
head = null;
}
public boolean isEmpty(){
return head == null;
}
public void insert(Object obj){
if(isEmpty()){
Node.child.add(obj);
head = new Node(obj,head);
}
}
public static void main(String args[])
{
Tree t=new Tree();
Integer j = null;
int i;
System.out.println("starting...");
for(i=0;i<5;i++){
j = new Integer((int)(Math.random() * 100));
t.insert(j);
System.out.println("insert: " + j);
}
System.out.println("Done ;-)");
}
}
-
January 24th, 2012, 05:03 AM
#12
Re: link Arraylist in java
If you post the code in code tags (see the blue text at the bottom of this post) and describe what the problem is I will look at it for you. Also, if there are any compiler errors or exceptions being thrown then post the full message and stack trace.
-
January 24th, 2012, 07:31 AM
#13
Re: link Arraylist in java
import java.util.*;
class Node{
protected Object data;
protected static ArrayList child;
protected Node next;
public Node(){
next = null;
data = null;
child=new ArrayList();
}
public Node(Object d,Node n){
data = d;
next = n;
n.child=new ArrayList();
}
}
public class Tree{
protected Node head;
public Tree(){
head = null;
}
public boolean isEmpty(){
return head == null;
}
public void insert(Object obj){
if(isEmpty()){
Node.child.add(obj);
head = new Node(obj,head);
}
}
public static void main(String args[])
{
Tree t=new Tree();
Integer j = null;
int i;
System.out.println("starting...");
for(i=0;i<5;i++){
j = new Integer((int)(Math.random() * 100));
t.insert(j);
System.out.println("insert: " + j);
}
System.out.println("Done ;-)");
}
}
the error message is:
C:\Users\Sugirthan\Desktop\Tree>javac Tree.java
Note: Tree.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
C:\Users\Sugirthan\Desktop\Tree>java Tree
starting...
Exception in thread "main" java.lang.NullPointerException
at Tree.insert(Tree.java:32)
at Tree.main(Tree.java:44)
C:\Users\Sugirthan\Desktop\Tree>
-
January 24th, 2012, 09:21 AM
#14
Re: link Arraylist in java
What happened to the code tags?
the error message is:
C:\Users\Sugirthan\Desktop\Tree>javac Tree.java
Note: Tree.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
This is not an error message it's a compiler warning message. It's because you are using an ArrayList without specifying what object type you want to put into it ie you are not using generics. It would be better if you did specify the type but it's not compulsory.
C:\Users\Sugirthan\Desktop\Tree>java Tree
starting...
Exception in thread "main" java.lang.NullPointerException
at Tree.insert(Tree.java:32)
at Tree.main(Tree.java:44)
The exception is being thrown because you are trying to access the ArrayList referenced by the 'child' field of the Node class but the field is null.
However, this is all irrelevant. Why have you made 'child' static it needs to be an instance field - every Node object needs it's own list of its children.
Why didn't you just use the code I provided as is?
-
January 24th, 2012, 11:19 AM
#15
Re: link Arraylist in java
import java.util.*;
class Node{
protected int data;
protected static ArrayList child;
protected Node next;
public Node(){
next = null;
child=new ArrayList();
}
public Node(int d,Node n){
data = d;
next = n;
n.child=new ArrayList();
}
}
public class Tree{
protected Node head;
public Tree(){
head = null;
}
public boolean isEmpty(){
return head == null;
}
public void insert(int obj){
if(isEmpty()){
Node.child.add(new Integer(1));
//head = new Node(obj,head);
}
}
public static void main(String args[])
{
Tree t=new Tree();
Integer j = null;
int i;
System.out.println("starting...");
for(i=0;i<5;i++){
j = new Integer((int)(Math.random() * 100));
t.insert(i);
//Node.child.add(new Integer(i));
System.out.println("insert: " + j);
}
System.out.println("Done ;-)");
System.out.println("size "+Node.child.size());
}
}
C:\Users\Sugirthan\Desktop\Tree>javac Tree.java
Note: Tree.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
C:\Users\Sugirthan\Desktop\Tree>java Tree
starting...
Exception in thread "main" java.lang.NullPointerException
at Tree.insert(Tree.java:30)
at Tree.main(Tree.java:42)
here i have commented the line(//head = new Node(obj,head) .but error message is appeared.why is error appeared?.here i have used child arraylist error message is appeared about you must use static type arraylist. here error message:
C:\Users\Sugirthan\Desktop\Tree>javac Tree.java
Tree.java:30: non-static variable child cannot be referenced from a static conte
xt
Node.child.add(new Integer(1));
^
Tree.java:47: non-static variable child cannot be referenced from a static conte
xt
System.out.println("size "+Node.child.size());
^
Note: Tree.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors
here i used clearly define childern as integer so i didnot use generic type arraylist
Last edited by sugir1987; January 24th, 2012 at 11:23 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|