2

for the following code which I worked on.Now the problem is how do I access the variable string outside the for loop?Thank you.

for (String[] string: arr) {
    if(string.length == 1)
    { 
        System.out.println(string[0]);
        continue;
    }
    for (int i = 1; i < string.length; i++)  {
        System.out.println(string[0] + " " + string[i]);
    }
}
5
  • 7
    You can't. It doesn't even make sense outside the loop. Which value would you expect to see? Commented Feb 12, 2013 at 19:03
  • 3
    Just because someone asks a quetion like this don't down rank him. That discourages learning. Commented Feb 12, 2013 at 19:04
  • I know I cant access it outside the loop.Is there any possible way to do so?? Commented Feb 12, 2013 at 19:06
  • 3
    Declare another variable outside the loop, and assign this variable the desired value before terminating the loop. (I'm not sure what "the desired value" is supposed to be in this case - that's the important bit for you work out :D) Commented Feb 12, 2013 at 19:06
  • Desired value should be string[0] and string[i].Declaring another variable does not work. Commented Feb 12, 2013 at 19:13

5 Answers 5

3

Your string variable is locally scoped and only exists within the loop. You need to define an external String[] variable first, then make an assignment to that variable within the loop:

String[] outsideString;

for (String[] string: arr) {
  ...
  outsideString = string;
  ...
}

// This line works
System.out.println(outsideString[0]);
Sign up to request clarification or add additional context in comments.

3 Comments

outsideString is of type String, string is of type String[], so this code won't compile.
Note that, in this case, you may well overwrite 'outsideString' several times while inside your loop. If you think you are going to need to access several Strings of interest that your loop processes, use a collection and add Strings you want outside of the loop to the collection.
maybe use += instead of just =
2

The following solution provides you the arraylist of all the Strings that are printed. This provides an arraylist created with the string array logic to use it beond the for loop.

Use the finalList to print all the Strings even after the for loop.

ArrayList<String> finalList = new ArrayList<String>();

    for (String[] string: arr) {

        if(string.length == 1)
        { 
            System.out.println(string[0]);
            finalList.add(string[0]);
            continue;
        }
        for (int i = 1; i < string.length; i++)  {
            System.out.println(string[0] + " " + string[i]);                
            finalList.add(string[0] + " " + string[i]);
        }   
    }

    for(String output: finalList){
        System.out.println(output);
    }

Hope this helps.

Comments

0

You can't given this code. In the enhanced for loop that you have at the top you can only use that variable local to that for loop

Comments

0

You can't. If you need to access something from the loop outside of said loop, create a String or Collection variable outside of the loop and assign/add to it from inside the loop.

Comments

0

Try this.

String[] outerString  = {}; 

    for (String[] string: arr) {
        outerString = string;
        if(string.length == 1)
        { 
            System.out.println(string[0]);
            continue;
        }
        for (int i = 1; i < string.length; i++)  {
            System.out.println(string[0] + " " + string[i]);
        }   
    }

    if(outerString.length > 0){
        System.out.println(outerString[0]);
    }

Do remember that only the last item in the Collection iteration will be printed in the outer string.

3 Comments

It works fine when its string[0]...but i need string[0]+ " "+ string[i] to be accessed outside the for loop..
Does this string[0]+ " "+ string[i] to be printed for all the iterations of the for loop?
yes,I should be printed for all the iterations of the for loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.