Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: What's wrong in my loop!!

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What's wrong in my loop!!

    Hey guys!

    I have a list array of integer type that consist of arrays with the index value of strings that I have stored in a string array. I want to get the actual string values for those indexes, so I created a set inside a loop that iterate over the different arrays stored in the list then another loop that iterates over every integer element inside the arrays. I want to use the set to store the string value of every index in the arrays. However, I'm only getting the set filled up with the correct values only for the first array. And I get all the strings printed inside the set of all the rest arrays.
    This is my code:

    //printing out the content of the clusters
    		       	    for(int i = 0; i < clusters.size(); i++){
    		       	    	System.out.println("cluster " + i + ":" + Arrays.toString(clusters.get(i)));
    				     for(int j = 0; j< clusters.get(i).length; j++){
     
     
    					    	ID_CLUSTER.add(STRINGS[clusters.get(i)[j]].toString());      
     
    				     } 
    			       	 System.out.println("cluster " + ID_CLUSTER);
                       }

    And this is what I get:
    cluster 0:[2, 0, 1]
    cluster ["This is another string", "A different string", "This is a string"]
    cluster 1:[3]
    cluster ["This is another string", "Short", "A different string", "This is a string"]
    cluster 2:[4]
    cluster ["This is another string", "A very very very very very very very very very very very long long long long string", "Short", "A different string", "This is a string"]

    I appreciate any advice!


  2. #2
    Junior Member
    Join Date
    May 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong in my loop!!

    Is this all of the code?

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong in my loop!!

    Off course no! This is only the loop part in which I need to store the strings value of each index in a string set. Why do u need to have all the code?

  4. #4
    Junior Member
    Join Date
    May 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong in my loop!!

    Not necessarily all of the code, but the array lists might help. Typically, if I don't see something right away in code, I dump it into my compiler and play around until I figure it out.

  5. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong in my loop!!

    Thanx for ur attention!! Actually I don't obtain the list array directly from a code that I wrote! I use an external library that returns the list array as an output of one of its methods. So trying to play with the code what i'm noting is that my loop creates adds the string values for the first array, and keep on adding the rest of values to that set. So basically, what I want to do is create an empty set after each loop to add only the values associated with the indexes stored in a specific array! Any idea about how to do that?

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What's wrong in my loop!!

    Please use standard Java terms so that we have some chance of understanding what you're asking. What's a "list array?" Do you mean ArrayList? Looking at the code, I think so, but I can't be sure what I'm looking at is what you're talking about.
    So basically, what I want to do is create an empty set after each loop to add only the values associated with the indexes stored in a specific array! Any idea about how to do that?
    The above objective statement is not clear. Again, I don't know what you mean by "empty set." A set is a collection that does not contain duplicates. Is that what you mean? Giving your variables better names would also be helpful.

    It "sounds" like you're using one collection to indicate the indices of which values to collect from another collection to store in a third collection. Something like:
        // method collectStrings() returns an array of Strings collected from the
        // argument ArrayList stringList at the indices specified by the elements
        // of the argument int[] indexArray
        public String[] collectStrings( int[] indexArray, ArrayList<String> stringList )
        {
            String[] resultArray = new String[indexArray.length];
     
            for ( int i = 0 ; i < indexArray.length ; i++ )
            {
                resultArray[i] = stringList.get( indexArray[i] );
            }
     
            return resultArray;
     
        } // end method collectStrings()
    Note that this example has no error checking.
    Last edited by GregBrannon; June 21st, 2013 at 04:44 AM. Reason: Correct typos.

  7. #7
    Junior Member
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong in my loop!!

    Quote Originally Posted by GregBrannon View Post
    Please use standard Java terms so that we have some chance of understanding what you're asking. What's a "list array?" Do you mean ArrayList? Looking at the code, I think so, but I can't be sure what I'm looking at is what you're talking about.

    The above objective statement is not clear. Again, I don't know what you mean by "empty set." A set is a collection that does not contain duplicates. Is that what you mean? Giving your variables better names would also be helpful.

    It "sounds" like you're using one collection to indicate the indices of which values to collect from another collection to store in a third collection. Something like:
        // method collectStrings() returns an array of Strings collected from the
        // argument ArrayList stringList at the indices specified by the elements
        // of the argument int[] indexArray
        public String[] collectStrings( int[] indexArray, ArrayList<String> stringList )
        {
            String[] resultArray = new String[indexArray.length];
     
            for ( int i = 0 ; i < indexArray.length ; i++ )
            {
                resultArray[i] = stringList.get( indexArray[i] );
            }
     
            return resultArray;
     
        } // end method collectStrings()
    Note that this example has no error checking.

    So sorry for not being able to explain my point! Yes list array == ArrayList.
    Here is exactly what I'm doing: I have an Array of type Object with some strings, I passed that array to a method where some calculations are done to group similar strings together. The output of that method is an ArrayList of type int[]. Now, I'm trying to loop through the int[] that are inside the list and then I'm using an inner loop to loop through each element of those arrays. What I want to get as a result is the strings grouped based on the indices that are in the int[].
    So I tried using an ArrayList of type String, and also tried the Set of type String to store the strings instead of their indices. What I'm seeing is that my loop adds the Strings to the Set or the ArrayList and keeps on adding the new values after each loop. While what I want to achieve is just adding only the Strings of specific indices. I hope I explained myself better this time. Looking forward hearing your comments.

Similar Threads

  1. Whats wrong with this for loop?
    By dx2731 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 26th, 2013, 10:33 AM
  2. My loop compiles and seems correct, but gives wrong output.
    By new2.java in forum What's Wrong With My Code?
    Replies: 8
    Last Post: February 15th, 2013, 08:35 PM
  3. My loop compiles and seems correct, but gives wrong output.
    By new2.java in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 14th, 2013, 10:31 PM
  4. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  5. I cannot get this loop to stop running. What am I doing wrong??
    By Nismoz3255 in forum Loops & Control Statements
    Replies: 2
    Last Post: May 1st, 2011, 10:28 PM