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 8785577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:24:12+00:00 2026-06-13T21:24:12+00:00

Why does think not work? It just prints zeros. However it works when I

  • 0

Why does think not work? It just prints zeros. However it works when I use a normal for loop with an index value ‘i’ and using ‘a[i]’ inside the body of the loop.

The problem is not with the printing loop, as it does not print the values, even with a normal for loop.

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

    Scanner s = new Scanner(System.in);
    int[] a = new int[5];
    for (int i : a)
    {
        System.out.println("Enter number : ");
        i=s.nextInt();

    }
    System.out.println("\nThe numbers you entered are : \n");
    for (int i : a)
    {
        System.out.println(i);
    }
}
}
  • 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-06-13T21:24:13+00:00Added an answer on June 13, 2026 at 9:24 pm

    When you access the element using enhanced for-loop: –

    for (int i : a)
    {
        System.out.println("Enter number : ");
        i=s.nextInt();
    
    }
    

    Here, int i is a copy of element in the array. When you modify it, the change will not get reflected in the array. That’s why the array elements are 0.

    So, you need to iterate using traditional for-loop and access the array elements on that index to assign values to it.

    Even if your array was an array of some reference, it would still not work. That’s because, the variable in a for-each is not a proxy for an array or Collection reference. For-each assigns each entry in the array to the variable in the loop.

    So, your enhanced for-loop: –

    for (Integer i: arr) {
        i = new Integer();
    }
    

    is converted to: –

    for (int i = 0; i < arr.length; i++) {
        Integer i = arr[i];
        i = new Integer();
    }
    

    So, the initialization of i in the loop, is not reflected in the array. And thus the array elements are null.

    Workarond: –

    1. use traditional for loop: –

      for (int i = 0; i < a.length; i++) {
          a[i] = sc.nextInt();
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

GLSL has a full C-style preprocessor. The only thing that does not work is
I think it's awesome how PasteBin highlights different languages, how does it work?
How does time.localtime() work exactly? I can call up the array (tupple, I think
I'm stunned by a simple thing that I want to accomplish but does not
and why does Implementation ID necessary in ECOM? UID3 is not enough? thank you.
Does anyone else think that the add view dialog in VS is useless or
Does anyone know off a way or maybe think its possible to connect Node.js
does List Sort modify the collection? I think it must as I get a
I think some of you have seen the cartoon wars game. does anybody know
I am rewriting a python script at work and part of what it does

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.