Results 1 to 7 of 7
  1. #1
    Arunkumar is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default Java LinkedList Issue

    Hi All,

    I have LinkedList "UserNodeList" with the node structure as below

    Class UserNode {
    int i;
    Node prevUserNode ;
    Node nextUserNode;
    }

    I need to create a new LinkedList from "UserNodeList" such that I create a new node "MyNode" from "UserNode".

    Class MyNode {
    int i;
    Node prevUserNode ;
    Node nextUserNode;
    }

    Could some one help me out?

  2. #2
    Abhij is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default

    can u be more clear . i couldn;t get your question properly

  3. #3
    Arunkumar is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default

    Hi Abhij,

    Thanks for looking in to it. let me eloborate my problem.

    Earlier, I had a collection of objects AUserOject submitted by the user in a Arraylist.

    Class AUserOject {

    ....
    ....
    }

    ArrayList AUserOjectList = new ArrayList();
    AUserOjectList .add(new AUserOject ());

    AUserOjectList .add(new AUserOject ())

    AUserOjectList .add(new AUserOject ())

    I need to convert this user typed collection into another such that
    For each "A" obj submitted by user, I need to create a new "AObject"

    Class AObject {

    ...
    ...

    }

    ArrayList list = new ArrayList();
    list.add(new AObject ());

    list.add(new AObject ())

    list.add(new AObject ())

    Hence I used to Iterate the Arraylist as below and create new object for each user object.

    ArrayList AOjectList = new ArrayList();
    Iterator<> iterator = AUserOjectList
    .iterator();
    while (iterator.hasnext() {
    AUserOjectList obj = (AUserOjectList )iterator.next
    AObject aobj = new AObject ();
    aobj.setvalue(obj.getValue);
    }

    Now, there is a requirement to have nextObject and prevObject for each object in list


    Class AUserOject {

    ....
    ....

    AUserOject prevObject;
    AUserOject nextObject;

    }

    How to I create a backend object from the list of User Objects

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Let me see if I understand you, You have two seperate classes, UserNode and MyNode which are used to make LinkedLists.

    Now you want to convert the List of UserNodes to make a List of MyNodes?

  5. #5
    Arunkumar is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default

    Yes. Exactly. Any Idea to fix this?

  6. #6
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Just have set methods do the work and set them accordingly.

    I'm not exactly sure how you are passing the lists but here's an idea to get you going.


    Java Code:
    /**Takes in the start node of a UserNode List and returns the start node
    	 * of a MyNode List
    	 */
    	public MyNode convertFromUserNode(UserNode startUserNode){
    		if (startUserNode != null) {
    			//Create the starting node for the MyNode List and set the index
    			MyNode startMyNode = new MyNode();
    			startMyNode.setIndex(startUserNode.getIndex());
    			
    			//Set the Current My Node and User Node for use in while statment
    			MyNode currentMyNode = startMyNode;
    			UserNode currentUserNode = startUserNode;
    			
    			//Cycles through the list setting the correct variables
    			while(currentUserNode.getNextNode() != null){
    				//Obtain the next User Node
    				UserNode nextUserNode = currentUserNode.getNextNode();
    				
    				//Create the next My Node and set the variables
    				MyNode nextMyNode = new MyNode();
    				nextMyNode.setIndex(nextUserNode.getIndex());
    				nextMyNode.setPreviousNode(currentMyNode);//Can set previous pointer cause its held outside loop
    				
    				//Set the previous index's pointer
    				currentMyNode.setNextNode(nextMyNode);
    				
    				//Update the current nodes to cycle through the list
    				currentMyNode = nextMyNode;
    				currentUserNode = nextUserNode; 
    			}
    			
    			//Return the start of the MyNode List
    			return startMyNode;
    		} //Parameter was null, return null
    		return null;		
    	}

  7. #7
    Abhij is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default U can use Array

    I guess the below implementation helps u

    import java.util.ArrayList;
    import java.util.Iterator;


    public class ArrayListTest {

    String nextNode;
    String prevNode;

    public static void main (String args[])
    {
    ArrayList ar1= new ArrayList();

    ar1.add("h1");
    ar1.add("h2");
    ar1.add("h3");
    ar1.add("h4");
    ar1.add("h5");


    String[] str=(String [])ar1.toArray(new String[ar1.size()]);

    ArrayList ar2=new ArrayList();


    for (int index=0;index<str.length;index++)
    {
    ArrayListTest at=new ArrayListTest();
    if(index==0)
    {
    at.prevNode=null;
    at.nextNode=str[index+1];
    ar2.add(at);
    continue;
    }
    if(index==str.length-1)
    {
    at.prevNode=str[index-1];
    at.nextNode=null;
    ar2.add(at);
    continue;
    }

    at.prevNode=str[index-1];
    at.nextNode=str[index+1];
    ar2.add(at);

    }

    System.out.println(ar2);
    }

    }


    This is just and idea u can replace with the exact java classes .


    Thanks
    abhijeet

Similar Threads

  1. Replies: 6
    Last Post: 07-16-2009, 04:30 PM
  2. php issue (a little different from java I know!)
    By thomase in forum New To Java
    Replies: 5
    Last Post: 04-01-2009, 02:02 AM
  3. LinkedList-java
    By kamali in forum New To Java
    Replies: 2
    Last Post: 09-26-2008, 04:29 PM
  4. Problem with LinkedList Java
    By paul in forum Advanced Java
    Replies: 2
    Last Post: 08-07-2007, 05:57 AM
  5. Please help me with LinkedList in Java
    By lenny in forum New To Java
    Replies: 2
    Last Post: 07-31-2007, 03:24 PM

Posting Permissions

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