Java 使用字符串的ArrayList作为参数编写方法

Java 使用字符串的ArrayList作为参数编写方法,java,methods,arraylist,Java,Methods,Arraylist,我正在尝试编写一个方法,该方法将字符串的ArrayList作为参数,并在每个长度为4的字符串前面放置一个由四个星号组成的字符串 然而,在我的代码中,我在构建方法的方式中遇到了一个错误 这是我的标记长度课程 import java.util.ArrayList; public class Marklength { void marklength4(ArrayList <String> themarklength){ for(String n : thema

我正在尝试编写一个方法,该方法将字符串的ArrayList作为参数,并在每个长度为4的字符串前面放置一个由四个星号组成的字符串

然而,在我的代码中,我在构建方法的方式中遇到了一个错误

这是我的标记长度课程

import java.util.ArrayList;


public class Marklength {

    void marklength4(ArrayList <String> themarklength){
        for(String n : themarklength){
            if(n.length() ==4){
                themarklength.add("****");
            }
        }
        System.out.println(themarklength);
    }

}
多谢各位。谢谢你的帮助

这个

if(n.length() ==4){
    themarklength.add("****");
}
正在尝试将
“**”
添加到列表的末尾。这会失败,因为
为每个
循环使用的
迭代器
不允许在对基础
列表
进行迭代时对其进行更改

您可以先创建
列表的副本

List<String> values = new ArrayList<String>(themarklength);
并使用这些作为迭代点

for (String value : values) {
接下来,您需要能够在特定点将新元素插入
ArrayList
。要做到这一点,您需要知道您正在使用的值的原始索引

if (value.length() == 4) {
    int index = themarklength.indexOf(value);
然后在所需位置添加新值

    themarklength.add(index, "****");
这将在
索引
点添加
“****”
,将所有其他条目向下推

已更新

正如已经正确地向我指出的那样,
marklength.indexOf(value)
的使用不会考虑
themarklength
列表包含相同值的两个元素的用例,这将返回错误的索引

我也没有把性能作为提供可能的解决方案的主要要求

已更新…

正如JohnGarnder和AnthonyAccioly所指出的,您可以对循环使用
,而不是对每个循环使用
,这样可以省去
的marklength.indexOf(value)

这将消除重复值扰乱索引位置的风险,并提高整体性能,因为您不需要创建第二个迭代器

// This assumes you're using the ArrayList as the copy...
for (int index = 0; index < themarklength.size(); index++) {
    String value = themarklength.get(index);
    if (value.length() == 4) {
        themarklength.add(index, "****");
        index++;
//假设您正在使用ArrayList作为副本。。。
对于(int index=0;index
但是你用什么取决于你……

这个

if(n.length() ==4){
    themarklength.add("****");
}
只是尝试将
“**”
添加到列表的末尾。此操作失败,因为
为每个
循环使用的
迭代器将不允许在对基础
列表进行迭代时对其进行更改

您可以先创建
列表的副本

List<String> values = new ArrayList<String>(themarklength);
并使用这些作为迭代点

for (String value : values) {
接下来,您需要能够在特定点将新元素插入到
ArrayList
中。为此,您需要知道正在使用的值的原始索引

if (value.length() == 4) {
    int index = themarklength.indexOf(value);
然后在所需位置添加新值

    themarklength.add(index, "****");
这将在
索引
点添加
“****”
,将所有其他条目向下推

已更新

正如已经正确地向我指出的那样,
marklength.indexOf(value)
的使用不会考虑
themarklength
列表包含相同值的两个元素的用例,这将返回错误的索引

我也没有把性能作为提供可能的解决方案的主要要求

已更新…

正如JohnGarnder和AnthonyAccioly所指出的,您可以对循环使用
,而不是对每个循环使用
,这样可以省去
的marklength.indexOf(value)

这将消除重复值扰乱索引位置的风险,并提高整体性能,因为您不需要创建第二个迭代器

// This assumes you're using the ArrayList as the copy...
for (int index = 0; index < themarklength.size(); index++) {
    String value = themarklength.get(index);
    if (value.length() == 4) {
        themarklength.add(index, "****");
        index++;
//假设您正在使用ArrayList作为副本。。。
对于(int index=0;index

但是你用什么取决于你…

哦,我记得在过去的好日子里有一个可爱的错误。问题是你的
ArrayList
并没有在访问数组元素的时候完全填充。想想看,你创建了对象,然后立即开始循环它。因此,对象本身必须填充values,因为循环将要运行

解决这个问题的简单方法是预先填充ArrayList

public class MarklengthTestDrive {
    public static void main(String[] args){

        ArrayList <String> words = new ArrayList<String>() {{ 

        words.add("Kane");
        words.add("Cane");
        words.add("Fame");
        words.add("Dame");
        words.add("Lame");  
        words.add("Same");
        }};
    }
}
公共类MarklengthTestDrive{
公共静态void main(字符串[]args){
ArrayList words=新的ArrayList(){
词语。添加(“凯恩”);
词语。添加(“甘蔗”);
词语。添加(“名誉”);
文字。添加(“女士”);
词语。添加(“Lame”);
词语。添加(“相同”);
}};
}
}

请告诉我这是否解决了问题。您也可以使用
静态
初始值设定项。

哦,我记得在过去的好日子里发生了这个可爱的错误。问题是,在访问数组元素时,您的
数组列表
并没有完全填充。想想看,您创建了对象,然后立即开始循环它因此,在循环运行时,对象必须用值填充自身

解决这个问题的简单方法是预先填充ArrayList

public class MarklengthTestDrive {
    public static void main(String[] args){

        ArrayList <String> words = new ArrayList<String>() {{ 

        words.add("Kane");
        words.add("Cane");
        words.add("Fame");
        words.add("Dame");
        words.add("Lame");  
        words.add("Same");
        }};
    }
}
公共类MarklengthTestDrive{
公共静态void main(字符串[]args){
ArrayList words=新的ArrayList(){
词语。添加(“凯恩”);
词语。添加(“甘蔗”);
词语。添加(“名誉”);
文字。添加(“女士”);
词语。添加(“Lame”);
词语。添加(“相同”);
}};
}
}

请告诉我这是否解决了问题。您也可以使用
静态
初始值设定项。

让我们考虑一下这段代码,并假装您没有遇到异常:

import java.util.ArrayList;


public class Marklength {

    void marklength4(ArrayList <String> themarklength){
        for(String n : themarklength){
            if(n.length() ==4){
                themarklength.add("****");
            }
        }
        System.out.println(themarklength);
    }
}
然后:

import java.util.ArrayList;


public class MarklengthTestDrive {
    public static void main(String[] args){

        ArrayList <String> words = new ArrayList<String>(); 

        words.add("Kane");
        words.add("Cane");
        words.add("Fame");
        words.add("Dame");
        words.add("Lame");  
        words.add("Same");

        Marklength ish = new Marklength();

        words = ish.marklength4(words);

    }
}
import java.util.ArrayList;
公共类MarklengthTestDrive{
    import java.util.ArrayList;

    public class MarkLength {
    void marklength4(ArrayList <String> themarklength){
        ArrayList<String> temp = new ArrayList<String>();
        for(String n : themarklength){
            if(n.length() ==4){
                temp.add(n);
                temp.add("****");
            }
        }
        themarklength.clear();
        themarklength.addAll(temp);
        System.out.println(themarklength);
    }
}