Results 1 to 3 of 3
  1. #1
    smtwtfs is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default How can I copy values 'only' to LinkedList/ArrayList?

    So I have been struggling with it for a long time, I started to realize Java may not be the best text processor in the world. ( should have used perl in the begining?) But I already have tons of programs in Java and its not worth moving.

    The question is, I am trying to get some statistical results on two parallel articles in two languages. I want a 2-D list, rows are the words appear in the first article, columns are the corresponding possible translations of the word in second article.

    I used LinkedList<linkedList<String>> for the 2-D List. What bothers me is that I always get the problem that only the pointers were copied, so as I modify the values, all the entries in the 2-D List become identical.

    Is there a way in LinkedList, that I can add values to LinkedList, and copy values from LinkedList? So that I can modify each variable without interfering other variables?

    Thank you all in advance.


    Java Code:
    // Desired 2-D list;
    LinkedList<LinkedList<String>> stats_w = new LinkedList<LinkedList<String>>();
    
    // temp list that adds entries to stats_w 
    LinkedList<String> temp_w = new LinkedList<String>;
    
    // this reads the input file
    String temp = in.readLine(); 
    String []temps = temp.split(" ");
    
    while(temp!=null){
    
    	int find_same = 0; // sort of boolean, indicates if duplicated entry found.
    
    	temps = temp.split(" ");
    	for(i = 0; i<stats_w.size(); i++){ // see if the entry exists
    		temp_w = stats_w.get(i);
    		//  ???  I believe this part is the problem, because every time I update temp_w, the stats_w gets updated too. 
    
    		if (temps[0]== ((String)temp_w.get(0)) ){
    			count++;
    			break;
    			find_same =1;
    		}
    	}
    	if(find_same==0){
    
    		// ???  because I need to use temp_w again, I want to clear it. However, stats_w is cleared too.
    		temp_w.clear();  
    
    		temp_w.add(temps[0]);
    		temp_w.add(temps[2]);
    		
    		// ??? Can I just add like this? does it add a new entry or it only adds the pointer?
    		stats_w.add(temp_w);
    
    		temp_w.clear();
    
    	}
    	temp = in.readLine();
    }
    Last edited by smtwtfs; 04-28-2011 at 09:39 AM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Simply change
    Java Code:
    temp_w.clear();
    to
    Java Code:
    temp_w = new LinkedList<String>();
    its faster anyway.

  3. #3
    smtwtfs is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    Thanks masijade it works fine, also save the trouble of copying back and forth.

    Java Code:
    LinkedList<String> temp = new LinkedList<String>();
    
    loop: from i = 0 to size of AListofList;
    {
       temp = AListofList[i];
       edit temp; // which updates AListofList[i];
       temp = new LinkedList<String>();
    }

Similar Threads

  1. ArrayList vs LinkedList
    By rp181 in forum New To Java
    Replies: 2
    Last Post: 02-27-2011, 03:08 AM
  2. Copy a List<Integer> to an ArrayList
    By Nosrettap in forum New To Java
    Replies: 3
    Last Post: 01-16-2011, 08:05 PM
  3. Replies: 1
    Last Post: 05-20-2010, 11:17 PM
  4. trying to set() values of in list of arraylist
    By alvations in forum New To Java
    Replies: 15
    Last Post: 10-13-2008, 10:35 PM
  5. Replies: 0
    Last Post: 12-05-2007, 03:09 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
  •