复制数组列表中的项目

我正在尝试克隆字符串中具有 char l 的项目并删除字符串中具有 char r 的项目,并且当 chars r 和 l 存在于同一字符串中时不执行任何操作,但是,当我实现我为克隆创建的 if 语句,但适用于其他所有内容。


我创建了一个 for 循环,它将循环遍历 arraylist 列表中的每个字符串。它将首先检查该索引中是否在字符串中同时存在 r 和 l,如果存在它将继续,然后它将检查字符串中是否存在 r 它将删除它然后返回字符串列表,如果不检查是否 l 然后在列表的最后一个索引中添加相同的元素。


package com.codegym.task.task07.task0716;


import java.util.ArrayList;


/* 

R or L


*/


public class Solution {

    public static void main(String[] args) throws Exception {

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

        list.add("rose"); // 0

        list.add("love"); // 1

        list.add("lyre"); // 2

        list = fix(list);


        for (String s : list) {

            System.out.println(s);

        }

    }


    public static ArrayList<String> fix(ArrayList<String> list) {

        for (int i = 0; i < list.size(); i++)

        {

            if (list.get(i).contains("r") && list.get(i).contains("l"))

            {

                continue;

            } else if (list.get(i).contains("r"))

            {

                list.remove(i);

            } else if (list.get(i).contains("l"))

            {

                list.add(list.size()-1,list.get(i));

            }

        }


        return list;

    }

}


UYOU
浏览 110回答 3
3回答

明月笑刀无情

首先,main在您的方法程序中和在接口中List(而不是ArrayList具体类型)。此外,您还可以使用它Arrays.asList来创建您的初始list. 喜欢,public static void main(String[] args) throws Exception {&nbsp; &nbsp; List<String> list = new ArrayList<>(Arrays.asList("rose", "love", "lyre"));&nbsp; &nbsp; list = fix(list);&nbsp; &nbsp; for (String s : list) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(s);&nbsp; &nbsp; }}然后,为了避免ConcurrentModificationException您需要使用Iterator.remove()(如 Javadoc 所述The behavior of an iterator is unspecified if the behavior of an iterator is unspecified if the underlying collection is used modified while the iteration is progress while the iteration is progress with any way other through the calling this method)。并追加创建要追加的项目的临时列表,然后在遍历List. 并且不要害怕将其存储value在临时变量中(list.get(index)既乏味又难以推理)。最后,您还可以使用变量保存“包含 l”和“包含 r”测试的结果。就像是,public static List<String> fix(List<String> list) {&nbsp; &nbsp; Iterator<String> iter = list.iterator();&nbsp; &nbsp; List<String> appendList = new ArrayList<>();&nbsp; &nbsp; while (iter.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; String value = iter.next();&nbsp; &nbsp; &nbsp; &nbsp; boolean containsR = value.contains("r"), containsL = value.contains("l");&nbsp; &nbsp; &nbsp; &nbsp; if (containsR && containsL) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; } else if (containsR) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iter.remove();&nbsp; &nbsp; &nbsp; &nbsp; } else if (containsL) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; appendList.add(value);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; list.addAll(appendList);&nbsp; &nbsp; return list;}哪个输出lovelyrelove

慕莱坞森

与其搞乱循环、迭代器和ConcurrentModificationExceptions,不如尝试使用流一个一个地执行这些步骤。首先,删除包含 r 但不包含 l 的字符串。一个简单的List#removeIf方法调用就足够了。//Remove strings that contain r but not llist.removeIf(s -> s.contains("r") && !s.contains("l"));现在让我们“克隆”包含 l 但不包含 r 的字符串。为此,我们将过滤列表并将结果收集到一个新的字符串列表中。然后调用List#addAll以将它们添加到原始列表中。//Find all strings that contain l but not rList<String> stringsThatContainLButNotR = list.stream().filter(s -> s.contains("l") && !s.contains("r"))&nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());list.addAll(stringsThatContainLButNotR);完整示例:public static void main(String[] args) {&nbsp; &nbsp; List<String> list = new ArrayList<>();&nbsp; &nbsp; list.add("rose"); // 0&nbsp; &nbsp; list.add("love"); // 1&nbsp; &nbsp; list.add("lyre"); // 2&nbsp; &nbsp; //Remove strings that contain r but not l&nbsp; &nbsp; list.removeIf(s -> s.contains("r") && !s.contains("l"));&nbsp; &nbsp; //Find all strings that contain l but not r&nbsp; &nbsp; List<String> stringsThatContainsLButNotR = list.stream().filter(s -> s.contains("l") && !s.contains("r"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());&nbsp; &nbsp; list.addAll(stringsThatContainsLButNotR);&nbsp; &nbsp; list.forEach(System.out::println);//prints "love","lyre","love"}

泛舟湖上清波郎朗

迭代时不要修改(添加/删除)列表。这也适用于其他集合类型。维护两个单独的列表 - 一个用于要添加的元素 ( addList),另一个用于要删除的元素( removeList)。最后,从主列表中删除必需的元素 ( removeList) 并添加其他必需的元素 ( addList)。我已经如上所述稍微修改了您的代码。import java.util.ArrayList;import java.util.List;public class DuplicateItemInList {&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> list = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; list.add("rose"); // 0&nbsp; &nbsp; &nbsp; &nbsp; list.add("love"); // 1&nbsp; &nbsp; &nbsp; &nbsp; list.add("lyre"); // 2&nbsp; &nbsp; &nbsp; &nbsp; list = fix(list);&nbsp; &nbsp; &nbsp; &nbsp; for (String s : list) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(s);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static ArrayList<String> fix(ArrayList<String> list) {&nbsp; &nbsp; &nbsp; &nbsp; List<String> addList = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; List<String> removeList = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < list.size(); i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (list.get(i).contains("r") && list.get(i).contains("l"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (list.get(i).contains("r"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; removeList.add(list.get(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (list.get(i).contains("l"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addList.add(list.get(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; list.removeAll(removeList);&nbsp; &nbsp; &nbsp; &nbsp; list.addAll(addList);&nbsp; &nbsp; &nbsp; &nbsp; return list;&nbsp; &nbsp; }}输出:lovelyrelove
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java