英文:
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<String> newList = new ArrayList<String>();
for(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;
}
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<String> newList = new ArrayList<String>();
for(String word : words) {
newList.add(word.replaceAll("[,.?:;]", ""));
}
words = newList; // if you want to replace the existing `words` list
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论