Java: Foreach loop not working as expected on int array?

I've got a pretty simple loop:

int[] positions = {1, 0, 0}

//print content of positions

for (int i : positions)
{
    if (i <= 0) i = -1;
}

//print content of positions

Now, what I would expect to get is:

array: 1, 0, 0
array: 1, -1, -1

but instead I get

array: 1, 0, 0
array: 1, 0, 0

Just... why?

Kind regards, jellyfish

Answers


Because "i" is a copy of an array element and not a reference to it :) You modify a local variable, not an array's element

this code is equivalent to

for(int index = 0; index < array.length; index++) {

int i = array[index];
...
}

Need Your Help

Xamarin.Forms C# find dominant color of image or image byte array

c# ios xamarin xamarin.forms

I'm using Xamarin.Forms to develop a cross-platform app. What is the best approach using c# with Xamarin to find the dominant color of an image? I found an ios approach:

Git push returning error and not prompting for password

git github

I followed the first steps after creating a repository in github, but when I got to the line:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.