如何在ArrayList中删除特定字符?

huangapple go评论109阅读模式
英文:

How would I go about removing a specific character in an ArrayList?

问题

我有一个ArrayList,其中包含如下所示的属性,其中一些单词后面有“,”、“.”和“?”。我想将这些字符替换为空,以删除它们。我尝试了类似以下的方法:

    ArrayList<String> newList = new ArrayList<String>();
    for (int i = 0; i < words.size(); i++) {
        if (words.get(i).contains(",")) {
            newList.add(words.get(i).replace(",", ""));
        } else {
            newList.add(words.get(i));
        }
    }
    words = newList;

但没有成功。ArrayList的名称是words。是否有我没有理解的解决方案?谢谢![JAVA]

为了更好理解,以下是我正在使用的ArrayList:

[=, A, ABILITY, ACCORDED, ACQUIRED, ADULT, AFTER, AGAIN, AGAINST, AGE, AGE, ALBERT, ALIENATED, ALL, ALMOST, ALTERED, ALWAYS, AM, AMONG, AN, AND, ANGER, ANY, ANYTHING, APPALLINGLY, APPROACH, ARE, ARE, ARTS, AS, ASSASSINATION, ASSERTED, AT, ATOM, ATTACHMENT, AUTHORITY, AVERAGE, AWARDED, AWE, BALANCE, BE, BE, BECAME, BECAUSE, BECOME, BEING, BEING, BELIEVING, BETTER, BEYOND, BICYCLE, BIG, BIGGER, BOMB, BORN, BOSOM, BOWL, BRANCHES, BUILT, BUT, BY, CALLED, CAN, CAUSE, CENTRAL, CENTURY, CENTURY, CHAIR, CHALLENGES, CHIEF, CHILDLIKE, CITIZENSHIP, CLEARLY, CLIMB, CLOSED, COLLECTION, COMMON, COMPLEX, CONCERN, COULD, COUNTED, COUNTED, COUNTS, COUNTS, COURAGE, CRAZY, CREATIVITY, CURIOSITY, CURIOUS, CURTAIL, DAYS, DEAD, DEEP, DEMANDS, DEPENDS, DEVELOPED, DEVELOPMENT, DEVOTES, DIAGRAMS, DID, DIFFICULTY, DIRECTION, DO, DOES, DOING, DRASTICALLY, DREAMS, DRIVES, DWELLS, DYING, E, EARLY, EDUCATION, EIGHTEEN, EINSTEIN, EINSTEIN’S, ELECTRIFYING, ELEMENTARY, ELSE, ENCIRCLES, ENCOUNTERED, ENDEAVORS, ENEMY, ENERGY, ENTITY, EQUATION, EQUATIONS, EVER, EVERYBODY, EVERYONE, EVERYTHING, EXCEEDED, EXCEL, EXISTING, EXPERIENCED, EXTENDED, EYES, FACT, FACTS, FACTS, FAMOUS, FAR, FATE, FEEL, FELT, FEW, FIELD, FIGHT, FIGURE, FINISHING, FIRST, FISH, FLOURISHED, FOOL, FOOLS, FOR, FOR, FORGET, FORGIVE, FORGOTTEN, FORM, FOUR, FROM, FRUIT, GARBAGE, GENIUS, GENIUS, GENIUS, GERMANY, GIVE, GLOBAL, GOOD, GRAMMAR, GREAT, GREATEST, GROUND-BREAKING, HAD, HAPPY, HAS, HAVE, HAZY, HE, HEARTS, HERITAGE, HIM, HIMSELF, HIS, HISTORY, HOW, HUMAN, HUMANITY, I, ICON, IF, IGNORE, IM, IMAGINATION, IMPORTANT, IN, INDIVIDUAL, INFLUENCED, INFLUENTIAL, INQUISITIVENESS, INSPIRING, INTELLIGENT, INTEREST, INTERVENE, INTO, IS, IS, ISAAC, IT, ITS, JERSEY, JEW, JEWISH, JUDGE, JUST, KEEP, KNACK, KNOWLEDGE, KNOWLEDGE, LARGELY, LATER, LEARNED, LEARNING, LEARNING, LEISURE]

英文:

I have an Arraylist with these attributes as shown below, and some of them have "," and "." and "?" after the words. I would like to replace the character with nothing, so as to remove them. I tried something like this:

    ArrayList&lt;String&gt; newList = new ArrayList&lt;String&gt;();
    for(i = 0; i &lt; words.size(); i++) {
        if (words.get(i).contains(&quot;,&quot;)) {
            newList.add(words.get(i).replace(&quot;,&quot;, &quot;&quot;));
        } else {
            newList.add(words.get(i));
        }
        words = newList;
    }

But to no avail. The ArrayList is called words. Is there a solution I'm not understanding? Thanks! [JAVA]

For context, here is the ArrayList I am working with:

>[=, A, ABILITY, ACCORDED, ACQUIRED, ADULT,, AFTER, AGAIN, AGAINST, AGE, AGE,, ALBERT, ALIENATED, ALL, ALMOST, ALTERED, ALWAYS, AM, AMONG, AN, AND, ANGER, ANY, ANYTHING, APPALLINGLY, APPROACH., ARE, ARE:, ARTS, AS, ASSASSINATION., ASSERTED, AT, ATOM, ATTACHMENT, AUTHORITY, AVERAGE, AWARDED, AWE,, BALANCE, BE, BE., BECAME, BECAUSE, BECOME, BEING, BEING,, BELIEVING, BETTER., BEYOND, BICYCLE., BIG, BIGGER, BOMB,, BORN, BOSOM, BOWL, BRANCHES, BUILT, BUT, BY, CALLED, CAN, CAUSE, CENTRAL, CENTURY, CENTURY,, CHAIR,, CHALLENGES,, CHIEF, CHILDLIKE, CITIZENSHIP, CLEARLY, CLIMB, CLOSED., COLLECTION, COMMON, COMPLEX., CONCERN, COULD, COUNTED, COUNTED., COUNTS, COUNTS,, COURAGE, CRAZY?, CREATIVITY., CURIOSITY, CURIOUS., CURTAIL, DAYS., DEAD;, DEEP, DEMANDS, DEPENDS, DEVELOPED, DEVELOPMENT, DEVOTES, DIAGRAMS, DID, DIFFICULTY, DIRECTION., DO., DOES, DOING, DRASTICALLY, DREAMS, DRIVES, DWELLS, DYING., E, EARLY, EDUCATION, EIGHTEEN., EINSTEIN, EINSTEIN’S, ELECTRIFYING, ELEMENTARY, ELSE, ENCIRCLES, ENCOUNTERED, ENDEAVORS., ENEMY, ENERGY., ENTITY, EQUATION,, EQUATIONS., EVER, EVERYBODY, EVERYONE, EVERYTHING, EXCEEDED, EXCEL, EXISTING., EXPERIENCED, EXTENDED, EYES, FACT,, FACTS,, FACTS., FAMOUS, FAR, FATE, FEEL, FELT, FEW, FIELD, FIGHT, FIGURE, FINISHING, FIRST, FISH, FLOURISHED., FOOL, FOOLS., FOR, FOR., FORGET, FORGIVE., FORGOTTEN, FORM, FOUR, FROM, FRUIT, GARBAGE, GENIUS, GENIUS,, GENIUS., GERMANY,, GIVE, GLOBAL, GOOD, GRAMMAR, GREAT, GREATEST, GROUND-BREAKING, HAD, HAPPY?, HAS, HAVE, HAZY:, HE, HEARTS., HERITAGE, HIM, HIMSELF, HIS, HISTORY, HOW, HUMAN, HUMANITY., I, ICON,, IF, IGNORE., IM, IMAGINATION, IMPORTANT, IN, INDIVIDUAL., INFLUENCED, INFLUENTIAL, INQUISITIVENESS, INSPIRING, INTELLIGENT, INTEREST, INTERVENE., INTO, IS, IS,, ISAAC, IT, ITS, JERSEY, JEW,, JEWISH, JUDGE, JUST, KEEP, KNACK, KNOWLEDGE, KNOWLEDGE., LARGELY, LATER, LEARNED, LEARNING, LEARNING,, LEISURE]

答案1

得分: 3

这就是你所需要的:

ArrayList<String> newList = new ArrayList<String>();
for(String word : words) {
    newList.add(word.replaceAll("[,.?:;]", ""));
}

words = newList; // 如果你想要替换现有的 `words` 列表

演示

英文:

This is all you need:

ArrayList&lt;String&gt; newList = new ArrayList&lt;String&gt;();
for(String word : words) {
  newList.add(word.replaceAll(&quot;[,.?:;]&quot;, &quot;&quot;));
}

words = newList; // if you want to replace the existing `words` list

Demo

huangapple
  • 本文由 发表于 2020年8月5日 07:21:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63256332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定