如何将列表中的两个项目合并为一个?

发布于 2024-11-29 01:46:10 字数 216 浏览 0 评论 0原文

假设我有以下列表:

A |乙| C | D|温度|是 | 100 | 100 °F |今天

我想将两个属性合并为一个,如下所示:

A |公元前 | D|温度|是 | 100°F |今天

我怎样才能实现这个目标?如果需要,可以更改集合。

Supposing I have the following list:

A | B | C | D or the | temperature | is | 100 | °F | today

I want to merge two attributes into one, something like this:

A | BC | D or the | temperature | is | 100°F | today

How can I achieve this? The Collection can be changed, if needed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绻影浮沉 2024-12-06 01:46:11

如果您想要做的是获取一个元素及其后继者并将它们合并,那么这应该可行:

String i = list.get(iIndex); 
String j = list.get(iIndex + 1);
i= i.concat(j);
list.set(iIndex,i);
list.remove(iIndex + 1);

if what your trying to do is take an element and its successor and merge them, this should work:

String i = list.get(iIndex); 
String j = list.get(iIndex + 1);
i= i.concat(j);
list.set(iIndex,i);
list.remove(iIndex + 1);
z祗昰~ 2024-12-06 01:46:11

我很惊讶没有标准的 API 方法可以做到这一点。哦,好吧,这是我自制的解决方案:

public static void main(String[] args) {
    final List<String> fruits = Arrays.asList(new String[] { "Apple", "Orange", "Pear", "Banana" });
    System.out.println(fruits); // Prints [Apple, Orange, Pear, Banana]
    System.out.println(merge(fruits, 1)); // Prints [Apple, OrangePear, Banana]
    System.out.println(merge(fruits, 3)); // Throws java.lang.IndexOutOfBoundsException: Cannot merge last element
}

public static List<String> merge(final List<String> list, final int index) {
    if (list.isEmpty()) {
        throw new IndexOutOfBoundsException("Cannot merge empty list");
    } else if (index + 1 >= list.size()) {
        throw new IndexOutOfBoundsException("Cannot merge last element");
    } else {
        final List<String> result = new ArrayList<String>(list);
        result.set(index, list.get(index) + list.get(index + 1));
        result.remove(index + 1);
        return result;
    }
}

I was surprised there was no standard API method to do this. Oh well, here's my home-brewed solution:

public static void main(String[] args) {
    final List<String> fruits = Arrays.asList(new String[] { "Apple", "Orange", "Pear", "Banana" });
    System.out.println(fruits); // Prints [Apple, Orange, Pear, Banana]
    System.out.println(merge(fruits, 1)); // Prints [Apple, OrangePear, Banana]
    System.out.println(merge(fruits, 3)); // Throws java.lang.IndexOutOfBoundsException: Cannot merge last element
}

public static List<String> merge(final List<String> list, final int index) {
    if (list.isEmpty()) {
        throw new IndexOutOfBoundsException("Cannot merge empty list");
    } else if (index + 1 >= list.size()) {
        throw new IndexOutOfBoundsException("Cannot merge last element");
    } else {
        final List<String> result = new ArrayList<String>(list);
        result.set(index, list.get(index) + list.get(index + 1));
        result.remove(index + 1);
        return result;
    }
}
感情洁癖 2024-12-06 01:46:11

没有直接的 API 可以实现这一点。但下面的内容可以帮助您开始

import java.util.ArrayList;
import java.util.List;

public class Test {

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

    /**
     * @param toIndex the index of the element that other items are merged with. The final merged item is set at this index.
     * @param indices a comma separated list of indices of the items that need be merged with item at <code>toIndex</code>
     */
    public static void merge(int toIndex, int... indices) {
        StringBuilder sb = new StringBuilder(list.get(toIndex));
        for (int i = 0; i < indices.length; i++) {
            sb.append(list.get(indices[i]));
        }
        for (int i = 0; i < indices.length; i++) {

            list.remove(indices[i]);
        }
        list.set(toIndex, sb.toString());
    }

    public static void main(String[] args) {
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        merge(1,2);
        System.out.println(list);
    }

}

There is no direct api for this. But the below can get you started

import java.util.ArrayList;
import java.util.List;

public class Test {

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

    /**
     * @param toIndex the index of the element that other items are merged with. The final merged item is set at this index.
     * @param indices a comma separated list of indices of the items that need be merged with item at <code>toIndex</code>
     */
    public static void merge(int toIndex, int... indices) {
        StringBuilder sb = new StringBuilder(list.get(toIndex));
        for (int i = 0; i < indices.length; i++) {
            sb.append(list.get(indices[i]));
        }
        for (int i = 0; i < indices.length; i++) {

            list.remove(indices[i]);
        }
        list.set(toIndex, sb.toString());
    }

    public static void main(String[] args) {
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        merge(1,2);
        System.out.println(list);
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文