使用字符串从arraylist中删除重复项

时间:2018-01-31 11:47:57

标签: java arraylist

我有一个看起来像这样的arraylist:

public static ArrayList<ArrayList<String[]>> x = new ArrayList<>();

我将2人组成一对。例如:

[Person1, Person2] [Person3, Person4]

我现在使用的算法仍然是重复的,我已经尝试了哈希映射并使用for循环迭代它们但是它们只是让我回原始列表。
这是代码:

package com.company;

import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

public class createGroups
{
public static ArrayList<ArrayList<String[]>> x = new ArrayList<>();

public static void main(String[] args){
    //Define names
    String[] names = {"Person1", "Person2", "Person3", "Person4"};
    try
    {
        //Create combinations. In a try catch because of the saveFile method.
        combination(names, 0, 2);
        //Print all the pairs in the Arraylist x
        printPairs();
    } catch (IOException e)
    {
        e.printStackTrace();
    }
}

static void combination(String[] data, int offset, int group_size) throws IOException
{
    if(offset >= data.length)
    {
        //Create new Arraylist called foo
        ArrayList<String[]> foo = new ArrayList<>();
        //Create a pair of 2 (data.length = 4 / group_size = 2)
        for(int i = 0; i < data.length / group_size; i++)
        {
            //Add the pair to foo.
            foo.add(Arrays.copyOfRange(data, 2 * i, 2 * (i + 1)));
        }
        //Add foo to x
        x.add(foo);
        //saveFile(foo);
    }

    for(int i = offset; i < data.length; i++){
        for(int j = i + 1; j < data.length; j++){
            swap(data, offset, i);
            swap(data, offset + 1, j);
            combination(data, offset + group_size, group_size);
            swap(data, offset + 1, j);
            swap(data, offset, i);
        }
    }
}

public static void printPairs(){
    //Print all pairs
    for(ArrayList<String[]> q : x){
        for(String[] s : q){
            System.out.println(Arrays.toString(s));
        }
        System.out.println("\n");
    }


}

private static void swap(String[] data, int a, int b){
    //swap the data around.
    String t = data[a];
    data[a] = data[b];
    data[b] = t;
}

}

现在输出是这样的: Output
每组4个名字都是成对的“列表”(不是真正的列表,但这就是我所说的)

这是所需的输出: Desired output
但是你可以看到第一个和最后一个对的列表基本相同,我如何在我的combination方法中改变它

问题:

  

如何更改combination方法,使其不会创建重复的组   如何在打印创建的列表时使列表更小(所需的输出)。

如果我不够清楚,或者我没有很好地解释我想要的东西,请告诉我。我会尽量让它更清楚。

1 个答案:

答案 0 :(得分:0)

创建一个与此类似的对象。它需要4个字符串(2对)。将字符串放入数组并对此数组进行排序。这意味着您输入的任何字符串组合都将转换为一个已排序的组合,但该对象内部会记住哪个人是person1,person2,......

private class TwoPairs {
    private final String person1;
    private final String person2;
    private final String person3;
    private final String person4;

    private final String[] persons;

    TwoPairs(String person1, String person2, String person3, String person4) {
        this.person1 = person1;
        this.person2 = person2;
        this.person3 = person3;
        this.person4 = person4;

        persons = new String[4];
        persons[0] = person1;
        persons[1] = person2;
        persons[2] = person3;
        persons[3] = person4;

        // if we sort array of persons it will convert 
        // any input combination into single (sorted) combination

        Arrays.sort(persons); // sort on 4 objects should be fast

        // hashCode and equals will be comparing this sorted array
        // and ignore the actual order of inputs
    }

    // compute hashcode from sorted array
    @Override
    public int hashCode() {
        return Arrays.hashCode(persons);
    }

    // objects with equal persons arrays are considered equal
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        TwoPairs other = (TwoPairs) obj;
        if (!Arrays.equals(persons, other.persons)) return false;
        return true;
    }

    // add methods which you might need
    // getters for individual persons
    // String getPerson1() { return person1; }
    // or perhaps pairs of persons
    // String[] getPair1() { return new String[] {person1, person2}; }
    // add sensible toString method if you need it
}

你的ArrayList x会像这样改变

ArrayList<TwoPairs> x = new ArrayList<TwoPairs>();

在将新的TwoPairs对象添加到x之前检查此列表是否已包含此对象。

if (!x.contains(twoPairsObject)) {
    x.add(twoPairsObject);
}