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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:33:36+00:00 2026-06-13T14:33:36+00:00

I think the code below will make my question clear. If they both pass

  • 0

I think the code below will make my question clear. If they both pass by value, why are they different.

C# below:

static void Main(string[] args)
    {
        var list = new List<int> {1, 2, 3,};
        ChangeVars(list);
        foreach (var i in list)
        {
            Console.WriteLine(i);
        }
        Console.Read();
    }

    private static void ChangeVars(List<int> list)
    {
        for(int i = 0; i < list.Count; i++)
        {
            list[i] = 23;
        }
    }

Returns 23, 23, 23,

Java below:

public static void main(String[] args)
{
    List<Integer> goods = new ArrayList<Integer>();
    goods.add(1); goods.add(2); goods.add(3);
    ChangeThings(goods);

    for(int item: goods)
    {
        System.out.println(item);
    }
}

private static void ChangeThings(List<Integer> goods)
{
    for(int item: goods)
    {
        item = 23;
    }
}

Returns 1, 2, 3.

I don’t understand the discrepancy.

  • 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-13T14:33:38+00:00Added an answer on June 13, 2026 at 2:33 pm

    You’re seeing the difference between a foreach loop and a regular for loop.
    Your Java code only assigns the local item variable; it doesn’t actually modify the list.

    If you write goods.set(i, 23), you will modify the list.

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

Sidebar

Related Questions

I think following code should generate an error: #include <iostream> #include <string> static void
The code below shows me (I think) that the for each loop is about
In the code below, I think I messed up this part: if ( (1
I've encountered code below and I'm not fully sure what it's achieving. I think
This code works perfectly. I only have one question, I want to make it
This is my below code, I am confuse why this thing is happening. In
I think this code For Each file in filecoll Ext = UCase(Right(File.Path, 3)) If
I think my code might be written incorrectly and this is causing my problem.
I don't think my code's the problem because it's working on my local server
I have this vb.net code ( but I think meaning code is equivalent for

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.