Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By Tolls

Thread: Strange problem with ArrayList .add() method

  1. #1
    X75TIGER75X is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Strange problem with ArrayList .add() method

    Hi, I'm trying to make program to load a text file with information inside. I've set up a while loop where program will collect all information and add it in String[] variable inside the loop. Once the String[] is complete, it will be added in ArrayList<String[]> using arrayList.add(informationArray). I thought it would work beautifully but I was wrong. According to the Debugger on jGrasp, after adding the string array in ArrayList, when program adds another string array to the ArrayList, it refuses to add a new index and overwrites already-created array at index 0 instead. Any solutions to this weird problem?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Strange problem with ArrayList .add() method

    Post an SSCCE...we have no clue what method(s) you are calling, how you are calling them, and how you are determining the List contents.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Strange problem with ArrayList .add() method

    Make sure you use an existing arraylist. Do not create a new one every time.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    X75TIGER75X is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Strange problem with ArrayList .add() method

    Alright, code is very long to post here, I'll post an important code section.

    First part of the code is to open and read "setting.txt" which contains save data with simple linebreak codes. # means break. #@\\ together means stop reading the file.

    setting.txt looks like this

    #SoftwareName#D:\Binary\SoftwareName\#data#App1#C: \Program Files (x86)\Test\App1#D:\Binary\SoftwareName\Repository# null##10598602622#App2#C:\Program Files (x86)\App2#D:\Binary\SoftwareName\Repository#null# #9352125739#App3#C:\Program Files (x86)\Test\App2#D:\Binary\SoftwareName\Repository# null##7940607241#@
    when program translates it, it will look more like this (Not actual java code)

    Java Code:
    -SoftwareName //Used to check if this file is designed only for this program
    -D:\Binary\SoftwareName\  //This is default path setting, dont mind about it
    -DATA //lets the program know it should start collecting input into ArrayList
    --"App1" //Title
    --"C:\Program Files (x86)\Test\App1" //folder path string
    --"D:\Binary\SoftwareName\Repository" //folder path string
    --null //empty information holder
    --"" //another empty information holder
    --"10598602622" //folder size in bytes
    ---adds all information in an String[] array and stores it in ArrayList using add() method
    --"App2" //Title
    --"C:\Program Files (x86)\Test\App2" //folder path string
    --"D:\Binary\SoftwareName\Repository" //folder path string
    --null //empty information holder
    --"" //another empty information holder
    --"9352125739" //folder size in bytes
    ---adds all information in an String[] array and stores it in ArrayList using add() method
    --"App3" //Title
    --"C:\Program Files (x86)\Test\App3" //folder path string
    --"D:\Binary\SoftwareName\Repository" //folder path string
    --null //empty information holder
    --"" //another empty information holder
    --"7940607241" //folder size in bytes
    -END //Tells reader to stop reading
    The second part of the code has loops that checks and validates all information in the text file and processes it. First, it will check for #SoftwareName# to check if textfile is correct. then it looks for default path string which is "D:\Binary\SoftwareName\". Then it detects #data# and starts process all strings into String[] then load that array into ArrayList. I hope I explained everything you need to know about this software.

    Here's my partial code. Look at line 31 for my problem.

    Java Code:
    try{
    			//Open
    			FileReader fr = new FileReader("setting.txt");
    			//Process
    			
    			int index1 = 0;
    			String[] information = {"", "", "", "", "", ""};
    			while(checker[2]){
    				int readIO = (char)fr.read();
    				inputString += "" + (char)readIO;
    				
    				if(inputString.equals("#SoftwareName#")){
    					inputString = "";
    					checker[0] = true;
    				}else if(checker[0] && (char)readIO == '#'){
    					int i = inputString.length();
    					defaultRepo = inputString.substring(0,(i-1));
    					System.out.println(defaultRepo);
    					inputString = "#";
    					checker[0] = false;
    					checker[1] = true;
    				}else if(inputString.equals("#data#")){
    					inputString = "#";
    				}else if(checker[1]){
    					if((char)readIO == '#'){
    						//Break
    						int i = inputString.length();
    						information[index1] = inputString.substring(1,(i-1));
    						inputString = "#";
    						if(index1 == 5){
    							data.add(information); //LOOK AT THIS - THIS DOESNT WORK PROPERLY
    							index1 = -1;
    						}
    						index1++;
    					}else if(inputString.equals("#@\\")){
    						checker[1] = false;
    						checker[2] = false;
    						
    					}
    				}
    			}
    			//Close
    			fr.close();
    		}catch(FileNotFoundException fnfe){
    			//File doesnt exist
    			System.out.println("File Doesnt Exist! Creating new file!");
    			fileDoesntExist = true;
    		}catch(IOException ioe){
    			//Bad Error
    		}
    I'll repeat my question here for you after you finish reading and trying to understand the concept of the code.
    According to the Debugger on jGrasp, after adding the string array in ArrayList, when program adds another string array to the ArrayList, it refuses to add a new index and overwrites already-created array at index 0 instead. Any solutions to this weird problem?
    Last edited by X75TIGER75X; 05-02-2012 at 04:23 AM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Strange problem with ArrayList .add() method

    Where is the arraylist: data defined and given a value? How often is the variable data given a value?
    Try debugging the code by adding a println after where data is given a value. Look at the print out to see if it is printed more than once.
    Add a println just before the data.add() statement that prints out the size of the arraylist to see if it ever has any elements in it before add() is called.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    X75TIGER75X is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Strange problem with ArrayList .add() method

    Quote Originally Posted by Norm View Post
    Where is the arraylist: data defined and given a value? How often is the variable data given a value?
    Try debugging the code by adding a println after where data is given a value. Look at the print out to see if it is printed more than once.
    Add a println just before the data.add() statement that prints out the size of the arraylist to see if it ever has any elements in it before add() is called.
    ArrayList is defined above the constructor, inside the class. It is defined as ArrayList<String[]> data = new ArrayList<String[]>; EDIT: Program compiles perfectly fine. data ArrayList is used on other places in the class without a problem.

    I've tried looking at Debugger, problem is still there. Let me show you

    when data.add(information) is invoked, debugger shows
    Java Code:
    data ->
    --[0] - String[]->
    -----------------[0]--"App1"
    -----------------[1]--"C:\Program Files (x86)\Test\App1"
    -----------------[2]--"D:\Binary\SoftwareName\Repository"
    -----------------[3]--null
    -----------------[4]--""
    -----------------[5]--"10598602622"
    --[1]
    when data.add(information) is invoked again, debuggers shows
    Java Code:
    data ->
    --[0] - String[]->
    -----------------[0]--"App2"
    -----------------[1]--"C:\Program Files (x86)\Test\App2"
    -----------------[2]--"D:\Binary\SoftwareName\Repository"
    -----------------[3]--null
    -----------------[4]--""
    -----------------[5]--"9352125739"
    --[1]
    Notice that the whole index 0 is replaced, index 1 did not get added at all, despite that .add() is used.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Strange problem with ArrayList .add() method

    Add the printlns, run the program and look at the print out.

    The code is creating a new data and perhaps a whole new containing class instead of using the existing one.

    A println will show you.
    Last edited by Norm; 05-02-2012 at 04:47 AM.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    X75TIGER75X is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Strange problem with ArrayList .add() method

    My Print-Out

    this file is correct because it has #SoftwareName#
    D:\Binary\SoftwareName\Repository
    default files storing done
    #data# - will start storing everything
    App1
    C:\Program Files (x86)\Test\Games\App1
    D:\Binary\SoftwareName\Repository
    null

    10598602622
    array added to ArrayList using add() method
    App2
    C:\Program Files (x86)\Test\App2
    D:\Binary\SoftwareName\Repository
    null

    9352125739
    array added to ArrayList using add() method
    App3
    C:\Program Files (x86)\Test\App3
    D:\Binary\SoftwareName\Repository
    null

    7940607241
    array added to ArrayList using add() method
    Data ArrayList size: 3
    Printing everything inside data ArrayList
    App3 C:\Program Files (x86)\Test\App3 D:\Binary\SoftwareName\Repository null 7940607241
    App3 C:\Program Files (x86)\Test\App3 D:\Binary\SoftwareName\Repository null 7940607241
    App3 C:\Program Files (x86)\Test\App3 D:\Binary\SoftwareName\Repository null 7940607241

    You can clearly see that it doesnt work properly. Also, it now has 3 ArrayList indexs this time. That's strange.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Strange problem with ArrayList .add() method

    Data ArrayList size: 3
    Do the last lines of the print out show 3 elements in data?

    Where are the print outs that show Data ArrayList size: with 1 and 2 elements?

    Where is the print out at the line where data was given a value? data = ....

    That's it for tonight. Back tomorrow.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    X75TIGER75X is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Strange problem with ArrayList .add() method



    Okay, I added in variables and etc in println to clear any confusion and questions.

    Java Code:
    String[] information = new String[6];
    this file is correct because it has #SoftwareName#
    D:\Binary\SoftwareName\Repository
    default files storing done
    #data# - will start storing everything
    information[0] = App1
    information[1] = C:\Program Files (x86)\Test\App1
    information[2] = D:\Binary\SoftwareName\Repository
    information[3] = SL
    information[4] = 
    information[5] = 90682392
    data.add(information);
    information[0] = ""
    information[1] = ""
    information[2] = ""
    information[3] = ""
    information[4] = ""
    information[5] = ""
    Information array cleared and ready to take new information
    information[0] = App2
    information[1] = C:\Program Files (x86)\App2
    information[2] = D:\Binary\SoftwareName\Repository
    information[3] = SL
    information[4] = 
    information[5] = 10598602622
    data.add(information);
    information[0] = ""
    information[1] = ""
    information[2] = ""
    information[3] = ""
    information[4] = ""
    information[5] = ""
    Information array cleared and ready to take new information
    information[0] = App3
    information[1] = C:\Program Files (x86)\App3
    information[2] = D:\Binary\SoftwareName\Repository
    information[3] = SL
    information[4] = 
    information[5] = 103833463
    data.add(information);
    Printing everything inside data ArrayList
    Data Element 1: App3 C:\Program Files (x86)\App3 D:\Binary\SoftwareName\Repository SL  103833463 
    Data Element 2: App3 C:\Program Files (x86)\App3 D:\Binary\SoftwareName\Repository SL  103833463 
    Data Element 3: App3 C:\Program Files (x86)\App3 D:\Binary\SoftwareName\Repository SL  103833463 
    Data elements (data.size() method): 3
    is this clear enough for you guys or do you need me to upload source code for you guys to test it yourself?
    Last edited by X75TIGER75X; 05-02-2012 at 05:45 AM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Strange problem with ArrayList .add() method

    I was expecting a print out like this:
    created data
    data.size() = 0
    data.size() = 1
    data contents = ....
    created data
    data.size() = 0
    ...

    What is printed when the data variable is given a value? data = ...

    Why are the contents of the information array being printed?

    I guess you should post the source.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Strange problem with ArrayList .add() method

    You have a single 'information' object.
    Each time round that loop you are changing the data inside the object.
    You then add that same reference to the array list.
    At no point (as far as I can see) do you actually initialise a new information object.
    Hence three entries all with the same data.
    X75TIGER75X likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Strange problem with ArrayList .add() method

    It's not clear then what the problem is.
    I thought that the problem was that the arraylist never got more than one element in it (max size = 1).
    The print out looks like the arraylist has three elements in it. (size = 3)
    As Tolls points out, the three elements all refer to the same object. Is that the problem you are having?

    For debugging I asked prints out the size of the arraylist. What was printed? What was the max size of the arraylist?
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    X75TIGER75X is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Strange problem with ArrayList .add() method

    Quote Originally Posted by Tolls View Post
    You have a single 'information' object.
    Each time round that loop you are changing the data inside the object.
    You then add that same reference to the array list.
    At no point (as far as I can see) do you actually initialise a new information object.
    Hence three entries all with the same data.
    I think you are right, I'll initialize a new information object inside the loop and try the code

    EDIT: Your solution works! Thank you for helping!
    Last edited by X75TIGER75X; 05-02-2012 at 07:20 PM.

Similar Threads

  1. Replies: 3
    Last Post: 03-28-2012, 12:23 AM
  2. Replies: 5
    Last Post: 09-13-2010, 12:02 PM
  3. Replies: 19
    Last Post: 08-10-2010, 09:36 AM
  4. strange problem
    By dinosoep in forum New To Java
    Replies: 6
    Last Post: 05-01-2010, 11:28 AM
  5. Strange problem
    By Tamir in forum Eclipse
    Replies: 1
    Last Post: 08-18-2008, 10:44 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
  •