Problem z listą list.

0

Witam,
mam kod funkcji deszyfrującej szyfr płotkowy napisany w C#. Potrzebuję go w programie z javy, niestety podczas przepisania tego kodu na jave występuje problem z listami gdyż nie mogę stworzyć listy w taki sam sposób jak tutaj, gdyż muszę wybierać pomiędzy ArrayList a LinkedList, a każde z tych rozwiązań stwarza problem podczas railFence[number].Add(i);

 
public static string Decrypt(int rail, string cipherText)
{
    int cipherLength = cipherText.Length;
    List<List<int>> railFence = new List<List<int>>();
    for (int i = 0; i < rail; i++)
    {
        railFence.Add(new List<int>());
    }
 
    int number = 0;
    int increment = 1;
    for (int i = 0; i < cipherLength; i++)
    {
        if (number + increment == rail)
        {
            increment = -1;
        }
        else if (number + increment == -1)
        {
            increment = 1;
        }
        railFence[number].Add(i);
        number += increment;
    }
 
    int counter = 0;
    char[] buffer = new char[cipherLength];
    for (int i = 0; i < rail; i++)
    {
        for (int j = 0; j < railFence[i].Count; j++)
        {
            buffer[railFence[i][j]] = cipherText[counter];
            counter++;
        }
    }
 
    return new string(buffer);
}

Będę wdzięczny za każdą podpowiedź.
Pozdrawiam

0

deklarujesz zmienna railFence jako List
List<List<int>> railFence = new List<List<int>>();
a odwolujesz sie do niej jak do tablicy co w java nie dziala
railFence[number].Add(i);

powinno byc

 
List<ArrayList<int>> railFence = new ArrayList<ArrayList<int>>();
railFence.get(number).add(i);
0
 
public static String Decrypt(int rail, String cipherText)
    {
        int cipherLength = cipherText.length();
        List<List<Integer>> railFence = new ArrayList<List<Integer>>();
        for (int i = 0; i < rail; i++)
        {
            railFence.add(new ArrayList<Integer>());
        }

        int number = 0;
        int increment = 1;
        for (int i = 0; i < cipherLength; i++)
        {
            if (number + increment == rail)
            {
                increment = -1;
            }
            else if (number + increment == -1)
            {
                increment = 1;
            }
            railFence.get(number).add(i);
            number += increment;
        }

        int counter = 0;
        char[] buffer = new char[cipherLength];
        for (int i = 0; i < rail; i++)
        {
            for (int j = 0; j < railFence.get(i).size(); j++)
            {
                buffer[railFence.get(i).get(j)] = cipherText.charAt(counter);
                counter++;
            }
        }

        return new String(buffer);
    }

Dzięki za pomoc. Śmiga.

1 użytkowników online, w tym zalogowanych: 0, gości: 1