Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ArrayList字符串循环将字符串弄乱_Java_Android_Arraylist - Fatal编程技术网

Java ArrayList字符串循环将字符串弄乱

Java ArrayList字符串循环将字符串弄乱,java,android,arraylist,Java,Android,Arraylist,我有一个叫PhotoArrayList的ArrayList。它包含“picture1.jpg、picture2.png、picture3.gif等”字符串。此ArrayList中大约有50或60个字符串。我需要在开头添加他们文件夹的路径,如“mnt/sdcard0/Pictures/picture1.jpg等”,因此,我使用以下代码 Integer PhotoFileAmount = PhotoArray.length; //PhotoArray and PhotoArrayList are s

我有一个叫PhotoArrayList的ArrayList。它包含“picture1.jpg、picture2.png、picture3.gif等”字符串。此ArrayList中大约有50或60个字符串。我需要在开头添加他们文件夹的路径,如“mnt/sdcard0/Pictures/picture1.jpg等”,因此,我使用以下代码

Integer PhotoFileAmount = PhotoArray.length; //PhotoArray and PhotoArrayList are same
for(int i=0; i < PhotoFileAmount; i++){
           String PhotoFileAndPath = (PhotoFolder + '/' + PhotoArrayList.get(i));
           PhotoArrayList.remove(PhotoArrayList.get(i));
           PhotoArrayList.add(PhotoFileAndPath);
       }
Integer PhotoFileAmount=PhotoArray.length//PhotoArray和PhotoArrayList是相同的
对于(int i=0;i

但我得到了一个奇怪的结果。PhotoArrayList的开头没有变化,中间部分没有问题,最后一部分得到了twitce路径。如“picture1.jpg、mnt/sdcard0/Pictures/picture2.png、mnt/sdcard0/Pictures/mnt/sdcard0/Pictures/picture3.gif”

如果要更改
ArrayList
的元素,请使用该方法为给定位置的元素指定一个新值:

int PhotoFileAmount = PhotoArray.length; // use int here to avoid unnecessary boxing / unboxing

for(int i=0; i < PhotoFileAmount; i++){
    String PhotoFileAndPath = (PhotoFolder + '/' + PhotoArrayList.get(i));
    PhotoArrayList.set(i, PhotoFileAndPath);
}
int-PhotoFileAmount=PhotoArray.length;//在此处使用int避免不必要的装箱/拆箱
对于(int i=0;i
如果使用
remove
add
不仅会更改列表中元素的索引;即使您使其工作起来,也会非常低效,因为每次调用
remove
时都必须移动列表中的所有剩余元素。

ArrayList.add(E对象)
对象添加到列表末尾的

您当前正在从列表中的任何位置删除项目,然后在列表末尾添加新版本。随着
i
接近
PhotoFileAmount
您的
。get(i)
语句将开始检索您在末尾添加的修订对象