For-Each and Pointers in Java

Posted by John on Stack Overflow See other posts from Stack Overflow or by John
Published on 2009-11-30T23:25:46Z Indexed on 2010/04/28 15:23 UTC
Read the original article Hit count: 226

Filed under:
|
|
|

Ok, so I'm tyring to iterate through an ArrayList and remove a specefic element. However, I am having some trouble using the For-Each like structure. When I run the following code:

ArrayList<String> arr = new ArrayList<String>();
//... fill with some values (doesn't really matter)

for(String t : arr)
{
  t = " some other value "; //hoping this would change the actual array
}

for(String t : arr)
{
  System.out.println(t); //however, I still get the same array here
}

My question in, how can I make 't' a pointer to 'arr' so that I am able to change the values in a for-each loop? I know I could loop through the ArrayList using a different structure, but this one looks so clean and readable, it would just be nice to be able to make 't' a pointer.

All comments are appreciated! Even if you say I should just suck it up and use a different construct.

© Stack Overflow or respective owner

Related posts about java

Related posts about foreach