对于-each和指针在Java中 [英] For-Each and Pointers in Java
问题描述
好了,所以我tyring通过一个ArrayList迭代并删除specefic元素。不过,我使用的for-each状结构遇到了一些麻烦。当我运行下面的code:
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
}
我的问题中,如何才能让'T'指针'改编',所以,我能够改变的值在for-each循环?我知道我可以通过循环使用不同的结构ArrayList中,但是这一次看起来如此干净和可读性,这纯粹是很好能够做出T的指针。
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.
所有评论都是AP preciated!即使你说我应该只吸起来,并使用不同的结构。
All comments are appreciated! Even if you say I should just suck it up and use a different construct.
推荐答案
我认为最好的办法可能是使用一个for循环。
I think the best approach may be to use a for loop.
ArrayList<String> arr = new ArrayList<String>();
for (int i = 0; i < arr.size(); i++) {
String t = arr.get(i);
if (// your condition is met) {
arr.set(i, "your new value");
}
}
这篇关于对于-each和指针在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!