Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6975703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:24:09+00:00 2026-05-27T17:24:09+00:00

I was working on a code and I came across this. How can I

  • 0

I was working on a code and I came across this. How can I use for-each in the below mentioned code to perform the same as the loop show exactly below (two nested for loops):

String names[3] = {"A","B","C"};
int result[][] = calculate_exchange(calculate_matrix());//function call returns a 3x3 matrix
        /*for(int i[]:result){
            for(int j:i){
                if(j!=0){
                    System.out.println(names[]);//how do I use the i'th element?
                    //names[i] gives an error(obviously!!!)
                }
            }
        }*/
        for(int r=0;r<3;r++){//this loop works fine
            for(int c=0;c<3;c++){
                if(result[r][c]!=0){
                    System.out.println(names[r]+"->"+names[c]+" = "+result[r][c]);
                }
            }
        }

for(int i[]:result) makes i an array, then would it be possible to use for-each in this case?

PS:I have got my code working without using for-each, i am asking this just to satisfy my curiosity.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:24:10+00:00Added an answer on May 27, 2026 at 5:24 pm

    From the Sun Java Docs:

    So when should you use the for-each loop?

    Any time you can. It really beautifies your code. Unfortunately, you cannot use it everywhere. Consider, for example, the expurgate method. The program needs access to the iterator in order to remove the current element. The for-each loop hides the iterator, so you cannot call remove. Therefore, the for-each loop is not usable for filtering. Similarly it is not usable for loops where you need to replace elements in a list or array as you traverse it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whilst working on some generally horrible Javascript code this morning, I came across the
I came across this code while working through code-analysis warnings on our code base.
I'm new to python and came across this segment of code. Can someone help
I was working on some code recently and came across a method that had
I came across the following code in a code base I am working on:
im working on application using delphi 7 , and i just came across this
I was working on some data parsing code while I came across the following.
I'm working on a Dashboard widget and I came across a code that looks
In the code I am currently working on I came across a piece code
today when I was working on some code of mine I came across a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.