简体   繁体   English

无法访问Java中的嵌套列表数组

[英]Trouble accessing nested Array of Lists in Java

Sorry for the clunky title, English is not my first language. 抱歉,标题笨拙,英语不是我的母语。

I'm having issues controlling how a nested for loop is going around a list of lists. 我在控制嵌套的for循环如何围绕列表列表时遇到问题。

Example: I have the letters, {A,B,C,D,E,F,G,H,I}. 示例:我有字母{A,B,C,D,E,F,G,H,I}。 They are in the 2d list like this: 它们在2d列表中是这样的:

 List<List<Character>> alphabet = new ArrayList<List<Character>>(3);
 alphabet.get(0).add('A'); 
 alphabet.get(1).add('B'); 
 alphabet.get(2).add('C'); 
 alphabet.get(0).add('D'); 
 alphabet.get(1).add('E'); 
 alphabet.get(2).add('F'); 
 alphabet.get(0).add('G'); 
 alphabet.get(1).add('H'); 
 alphabet.get(2).add('I');

So every 3rd letter in the alphabet goes in the same list. 因此,字母表中的每个第3个字母都在同一列表中。 I'm having a few problems making the for loop that would go through the alphabet list and reconstruct the alphabet in the correct order. 我在制作for循环时遇到一些问题,该循环将遍历字母表列表并以正确的顺序重建字母表。 Any help would be appreciated. 任何帮助,将不胜感激。

Which would be the best solution, to go across and get the letters in order, A then B then C then D. 这是最好的解决方案,先按顺序获取字母,依次为A,然后B,C,然后是D。

Or go down each list first, get A then D then G, and when I move to the next list, slot then into the required position? 还是先浏览每个列表,先获得A,然后获得D,再获得G,然后当我移至下一个列表时,将其插入所需的位置?

Thank you 谢谢

// Rephrased //改写

Imagine 3 lists, in a list. 想象一个列表中的3个列表。 The 3 lists represent columns of letters. 3个列表代表字母列。

alphabet = [[a, d, g, j], [b, e, h], [c, f, i]]

What is the best way to loop through the nested list, and reconstruct the alphabet in the correct order? 遍历嵌套列表并按正确顺序重建字母的最佳方法是什么?

I will then put the result in a string and write it to a file. 然后,我将结果放入字符串中并将其写入文件。

This is my for loop. 这是我的for循环。

for(int k = 0, i = 0; k < decryptedtextColumns.get(i).size(); k++)
{
    for(i = 0; i < decryptedtextColumns.size(); i++)
    {
        if(k <= decryptedtextColumns.get(i).size())
        {
            Character letter = decryptedtextColumns.get(i).get(k);
            decryptedtext.add(letter);
        }
    }
} 

Java 8 has a simple and short way to do it. Java 8有一个简单而简短的方法。

 List<Character> sortedAlphabets= alphabet.stream().flatMap(list->list.stream()).sorted().
                 collect(Collectors.toList());

produces output 产生输出

[A, B, C, D, E, F, G, H, I]

You'll have to use the MOD operator and do arithmetics on char. 您将不得不使用MOD运算符并对char进行算术运算。

Here is a possible solution : 这是一个可能的解决方案:

     for (int i = 0 ; i < 26 ; i++){
         alphabet.get(i%3).add((char) ('A' + i));
     }
  • You actually use the MOD of i%3 to go systematically from 0 to 2 and add the value of char 'A' + i each time you loop. 实际上,你使用MODi%3从系统进入02 ,并添加值char 'A' + i每次循环。

You basically just need a multi-dimensional form of the merge function from the mergesort algorithm. 您基本上只需要合并排序算法中合并函数的多维形式。

Check out the following code at IdeOne . IdeOne处检查以下代码。

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        List<List<Character>> alphabet = new ArrayList<List<Character>>(3);
        List<Character> first = new ArrayList<Character>();
        first.add('a');
        first.add('d');
        first.add('g');
        first.add('i');
        List<Character> second = new ArrayList<Character>();
        second.add('b');
        second.add('e');
        second.add('h');
        List<Character> third = new ArrayList<Character>();
        third.add('c');
        third.add('f');
        third.add('j');
        alphabet.add(first);
        alphabet.add(second);
        alphabet.add(third);

        List<Character> mergedSoFar = new ArrayList<Character>();
        for(int i = 0; i < alphabet.size(); ++i) {
            List<Character> sortedLetters = alphabet.get(i);
            mergedSoFar = merge(mergedSoFar, sortedLetters);
        }
        System.out.println(mergedSoFar);
    }

    private static List<Character> merge(List<Character> left, List<Character> right) {
        List<Character> merged = new ArrayList<Character>(left.size() + right.size());
        int i = 0;
        int j = 0;
        while (i < left.size() && j < right.size()) {
            char leftLetter = left.get(i);
            char rightLetter = right.get(j);
            int comparison = Character.compare(leftLetter, rightLetter);
            if (comparison < 0) {
                merged.add(leftLetter);
                ++i;
            } else if (comparison > 0) {
                merged.add(rightLetter);
                ++j;
            } else {
                // letters are same, add both
                merged.add(leftLetter);
                ++i;
                merged.add(rightLetter);
                ++j;
            }
        }
        while(i < left.size()) {
            merged.add(left.get(i));
            ++i;
        }
        while(j < right.size()) {
            merged.add(right.get(j));
            ++j;
        }
        return merged;
    }
} 

Managed to get it working. 设法使其工作。 As its a very specific thing I'm not sure how much use it will be but the general case is as explained above. 作为一个非常具体的事情,我不确定会使用多少,但一般情况如上所述。

for(int k = 0, i = 0; k < decryptedtextColumns.get(i%5).size(); k++)
    {           
        i = 0;
        while(i < decryptedtextColumns.size() && k < decryptedtextColumns.get(i).size())
        {
            if(k <= decryptedtextColumns.get(i).size())
            {
                Character letter = decryptedtextColumns.get(i).get(k);
                decryptedtext.add(letter);
            }
            i++;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM