Java 查找用户定义对象的ArrayList的ArrayList的所有组合

Java 查找用户定义对象的ArrayList的ArrayList的所有组合,java,arraylist,nested-lists,Java,Arraylist,Nested Lists,我定义了一个类Note,它表示演奏某个音符的方式(弦乐器上弦乐和弗雷特的两个整数),以及一个类Chord,它有一个该和弦中所有音符的数组列表 对于播放的每个音符,可能有多种播放方式,因此我有一个表示每种可能方式的音符数组列表。在和弦中可以有任意数量的音符,所以我有一个音符的数组列表。从这里,我想创建一个和弦阵列列表,列出每种可能的和弦演奏方式 我已经定义了一个构造函数Chord(arraylistnotes) 例如: 音符A有3种演奏方式,音符B有2种演奏方式,因此我想要和弦: [A1,B1]、

我定义了一个类
Note
,它表示演奏某个音符的方式(弦乐器上弦乐和弗雷特的两个整数),以及一个类
Chord
,它有一个该和弦中所有音符的数组列表

对于播放的每个音符,可能有多种播放方式,因此我有一个表示每种可能方式的音符数组列表。在和弦中可以有任意数量的音符,所以我有一个音符的数组列表。从这里,我想创建一个和弦阵列列表,列出每种可能的和弦演奏方式

我已经定义了一个构造函数
Chord(arraylistnotes)

例如: 音符A有3种演奏方式,音符B有2种演奏方式,因此我想要和弦:

[A1,B1]、[A1,B2]、[A2 B1]、[A2 B2]、[A3,B1]、[A3,B2]

我已经创建了一个方法,它的工作原理是假设始终播放3个音符,但无法思考如何将其扩展到未知数字

public static ArrayList<Chord> allPlayable(ArrayList<ArrayList<Note>> candidates)
{
    ArrayList<Chord> allPlayable = new ArrayList<>();

    for (int i = 0; i < candidates.get(0).size(); i++)
    {
        Note n0 = candidates.get(0).get(i);

        for (int j = 0; j < candidates.get(1).size(); j++)
        {
            Note n1 = candidates.get(1).get(j);

            for (int k = 0; k < candidates.get(2).size(); k++)
            {
                Note n2 = candidates.get(1).get(k);


                ArrayList<Note> chordNotes = new ArrayList<>();

                chordNotes.add(n0);
                chordNotes.add(n1);
                chordNotes.add(n2);

                allPlayable.add(new Chord(chordNotes));

            }
        }
    }

    return allPlayable;
}
公共静态ArrayList AllPlay(ArrayList候选)
{
ArrayList allPlayable=新的ArrayList();
for(int i=0;i
有人建议我使用递归-每个for循环都是另一个递归调用,我给出了这个答案

public static ArrayList<Chord> allPlayable(ArrayList<ArrayList<Note>> candidates)
{
    //this will be the inner ArrayList we are on 
    ArrayList<Note> current = new ArrayList();

    //the list of chords to return
    ArrayList<Chord> allPlayable = new ArrayList();

    allPlayableRecurse(candidates, 0, current, allPlayable);

    return allPlayable;
}
public static void allPlayableRecurse(ArrayList<ArrayList<Note>> candidates, int index, ArrayList<Note> chordNotes, ArrayList<Chord> allPlayable)
{
    ArrayList<Note> current = candidates.get(index);

    //for each note in the current array list of notes
    for (int i = 0; i < current.size(); i++)
    {
        chordNotes.add(current.get(i));

        //there are more notes to add
        if (index < candidates.size()-1)
        {
            //go to the next inner ArrayList
            allPlayableRecurse(candidates, index+1, chordNotes, allPlayable);
        }
        else//we have reached the last note
        {
            //add the chord to the list
            allPlayable.add(new Chord((ArrayList<Note>)chordNotes.clone()));
        }

        //we will now replace this note
        chordNotes.remove(chordNotes.size()-1);
    }
}
公共静态ArrayList AllPlay(ArrayList候选)
{
//这将是我们正在使用的内部阵列列表
ArrayList当前=新的ArrayList();
//要返回的和弦列表
ArrayList allPlayable=新的ArrayList();
所有可播放递归(候选,0,当前,所有可播放);
返回所有可玩的;
}
公共静态void allPlayable递归(ArrayList候选项、int索引、ArrayList弦节点、ArrayList allPlayable)
{
ArrayList current=candidates.get(索引);
//对于当前注释数组列表中的每个注释
对于(int i=0;i
您可以使用递归,例如:

   List<List<Note>> combine(List<List<Note>> representations) {
        List<Note> options = representations.get(0);
        List<List<Note>> tails;
        if (representations.size()==1) {
            tails = new ArrayList<>();
            tails.add(Collections.emptyList());
        } else {
            tails = combine(representations.subList(1, representations.size()));
        }

        List<List<Note>> combinations = new ArrayList<>(options.size());
        for (Note note : options) {
            for (List<Note> tail : tails) {
                List<Note> chord = new ArrayList<>();
                chord.add(note);
                chord.addAll(tail);
                combinations.add(chord);
            }

        }
        return combinations;

    }

    public List<Chord> allPlayable(List<List<Note>> candidates) {

        List<List<Note>> combinations = combine(candidates);
        List<Chord> chords = new ArrayList<>(combinations.size());
        for (List<Note> notes : combinations) chords.add(new Chord(notes));
        return chords;      
列表组合(列表表示){
列表选项=表示。获取(0);
列出尾部;
if(表示法.size()==1){
tails=newarraylist();
tails.add(Collections.emptyList());
}否则{
tails=combine(representations.subList(1,representations.size());
}
列表组合=新的ArrayList(options.size());
用于(注:选项){
对于(列表尾部:尾部){
List chord=new ArrayList();
和弦。加(注);
和弦.addAll(尾);
组合。添加(和弦);
}
}
收益组合;
}
公共列表所有可播放(列出候选人){
列表组合=组合(候选);
列表和弦=新的ArrayList(combines.size());
对于(列表注释:组合)和弦。添加(新和弦(注释));
回音和弦;
}


这是旧的java7风格。使用新的Java8功能操作可以更容易地完成,但如果您刚刚开始,则不确定它是否已经是您的事情。

您的问题是什么?你试过什么代码?你会犯什么错误?如果这是一个家庭作业问题,这不是问它的地方。对不起,如果我不清楚。我正在尝试创建一个方法,返回一个和弦对象的ArrayList,这个ArrayList应该有所有可能的组合,用所有给定的音符演奏和弦。如果它是一个2d注释矩阵,我知道嵌套的for循环可以用于覆盖所有可能的结果,但是我不确定使用什么,因为所有ArrayList的大小都是动态的。您仍然需要尝试一些代码并将其发布在此处。所以它不是为您编写代码的。尝试一下,包括你的代码和任何错误。我已经为3个注释创建了一个解决方案,但不知道如何扩展它以允许动态大小。我应该澄清一下,我不是在寻求解决方案,我只是不知道如何开始自己创建解决方案。