CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Jan 2012
    Posts
    30

    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?

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: link Arraylist in java

    Quote Originally Posted by sugir1987 View Post
    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.

  3. #3
    Join Date
    Jan 2012
    Posts
    30

    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

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Jan 2012
    Posts
    30

    Re: link Arraylist in java

    i cantn't get. can you post the code?

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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>>();
    }
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Jan 2011
    Posts
    24

    Re: link Arraylist in java

    Are you trying to draw Tree in Swing ?

  8. #8
    Join Date
    Jan 2012
    Posts
    30

    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).

  9. #9
    Join Date
    Jan 2011
    Posts
    24

    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

  10. #10
    Join Date
    Jan 2012
    Posts
    30

    Re: link Arraylist in java

    thanks a lot

  11. #11
    Join Date
    Jan 2012
    Posts
    30

    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 ;-)");

    }
    }

  12. #12
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  13. #13
    Join Date
    Jan 2012
    Posts
    30

    Question 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>

  14. #14
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  15. #15
    Join Date
    Jan 2012
    Posts
    30

    Question 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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured