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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:56:46+00:00 2026-05-11T13:56:46+00:00

In this code, why isn’t my array initialised as I want it to? Is

  • 0

In this code, why isn’t my array initialised as I want it to? Is the for-each loop not designed to do that, or am I just not using it correctly?

    int[] array = new int[5];      //initialise array -> Doesn't work! Array still full of 0's     for(int i : array)         i = 24; 
  • 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. 2026-05-11T13:56:47+00:00Added an answer on May 11, 2026 at 1:56 pm

    The for-each loop will not work for this case. You cannot use a for-each loop to initialize an array. Your code:

    int[] array = new int[5]; for (int i : array) {     i = 24; } 

    will translate to something like the following:

    int[] array = new int[5]; for (int j = 0; j < array.length; j++) {     int i = array[j];     i = 24; } 

    If this were an array of objects, it would still fail. Basically, for-each assigns each entry in the collection or array, in turn, to the variable you provide, which you can then work with. The variable is not equivalent to an array reference. It is just a variable.

    For-each cannot be used to initialize any array or Collection, because it loops over the current contents of the array or Collection, giving you each value one at a time. The variable in a for-each is not a proxy for an array or Collection reference. The compiler does not replace your ‘i‘ (from ‘int i‘) with ‘array[index]‘.

    If you have an array of Date, for example, and try this, the code:

    Date[] array = new Date[5]; for (Date d : array) {     d = new Date(); } 

    would be translated to something like this:

    Date[] array = new Date[5]; for (int i = 0; i < array.length; i++) {     Date d = array[i];     d = new Date(); } 

    which as you can see will not initialize the array. You will end up with an array containing all nulls.

    NOTE: I took the code above, compiled it into a .class file, and then used jad to decompile it. This process gives me the following code, generated by the Sun Java compiler (1.6) from the code above:

    int array[] = new int[5]; int ai[]; int k = (ai = array).length; for(int j = 0; j < k; j++) {     int i = ai[j];     i = 5; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It depends whether the list of the added functionlities is… May 11, 2026 at 11:44 pm
  • Editorial Team
    Editorial Team added an answer CREATE PROCEDURE UpdateTables AS BEGIN -- SET NOCOUNT ON added… May 11, 2026 at 11:44 pm
  • Editorial Team
    Editorial Team added an answer I've managed to simulate this situation: To do a normal… May 11, 2026 at 11:44 pm

Related Questions

In this code, why isn't my array initialised as I want it to? Is
This code compiles: private static void Main(string[] args) { bool? fred = true; if
I'm having issues with internet Explorer. I have a link which duplicates a file
I'm running Python 2.5, so this question may not apply to Python 3. When
I have a subclass with an over-ridden method that I know always returns a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.