i have two arraylist. i have to link the one arraylist with another arraylist's perticular index. can you help me?
Printable View
i have two arraylist. i have to link the one arraylist with another arraylist's perticular index. can you help me?
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
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.
i cantn't get. can you post the code?
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>>();
}
Are you trying to draw Tree in Swing ?
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).
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
thanks a lot
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 ;-)");
}
}
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.
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>
What happened to the code tags?
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.Quote:
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.
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.Quote:
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)
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?
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
Why can you not post your code surrounded by code tags, this is the third time I have had to ask you. The bottom of everyone of my posts shows how to do it - see the blue text.
In future I will not answer posts from you unless the code is wrapped in code tags.
The Node classes 'child' field must NOT be declared as static. Static means there will be just one child ArrayList shared by every object of type Node which means every Node object will be at the same level. This is wrong, every Node object must have it's own list of its children - so remove the static modifier.
The problem is with how you are trying to access the Node's child list. You are using the Node class ie Node.child when you should be using a Node object ie myNode.child.
Your code appears to be full of problems but unless it is formatted properly I'm not going to try to wade thought it to figure out what you are doing wrong.
You haven't defined 'childern' at all. You have declared a variable 'child', which is badly named as it doesn't reference a child at all but rather it references a collection of children hence I called it children.Quote:
here i used clearly define childern as integer so i didnot use generic type arraylist
You have declared your data type as int which means you don't need to make the Node class generic but the ArrayList is a generified class so you have to specify what type you are putting in it or you will get the compiler warnings. ie:
Code:protected ArrayList<Node> child;
...
child = new ArrayList<Node>();
thanks a lot. your link go to http://www.keang.co.uk/. here i can't
find the code. can you post the entire code. because i have to understand the code.
This web site contains fully featured classes and code examples that may be useful to other, all the code can be found by selecting the appropriate menu on the left hand side.Quote:
What code were you looking for?
No.Quote:
can you post the entire code.
You learn by writing code yourself and not by getting others to write it for you.Quote:
because i have to understand the code.
thanks a lot i have almost finished my work using your suggestion