简体   繁体   English

链表Java数组

[英]Array of Linked List Java

So I have been trying to fix this by myself but I didn't find enough info on the subject. 因此,我一直在尝试自己解决此问题,但是我没有找到足够的信息。

In the next code, there is a function that receives an array of a linked list (Integer), the array is a representation of a square that has black and white squares inside (1 = white, 0 = black), the format is the next one: The first node of the linked list is white, every next node is the opposite color of the last node. 在下一个代码中,有一个函数接收链接列表的数组(整数),该数组表示内部具有黑白方块(1 =白色,0 =黑色)的正方形的表示形式,格式为下一个:链接列表的第一个节点为白色,每个下一个节点与最后一个节点的颜色相反。 For example if the square is: white -> white -> white -> black - > white - > black -> black the linked list would be 3 -> 1 -> 1 -> 2 -> null (if there are consecutive colors they sum up in the linked list as seen before). 例如,如果正方形是:白色->白色->白色->黑色->白色->黑色->黑色,则链接列表将为3-> 1-> 1-> 2->空(如果存在连续的颜色)它们汇总在链接列表中,如前所示)。 So my code is the next: 所以我的代码是下一个:

public static int[][] restorePicture (LinkedList[] linked_list) 
{
    boolean black = false;
    int[][] Input = new int [(linked_list.length)][];

    for(int k = 0; k < linked_list.length; k++)
        Input[k] = new int[linked_list[k].size()];

    for(int i = 0;i < linked_list.length; i++)
    {
        black = false; 
        int j = 0;
        while(linked_list[i].get(j) != linked_list[i].getLast())
        {
            if(black == false)
            {
                for(int z = (int) linked_list[i].get(j); z > 0 ;z--)
                    Input[j++][i] = 1;

                black = true;
            }

            if(black == true)
            {
                for(int x = (int) linked_list[i].get(j); x > 0 ;x--)
                    Input[j++][i] = 0;

                black = false;
            }
        }
    }

    for(int i = 0; i < Input.length; i++)
        for(int j = 0; j < Input[j].length; j++)
            System.out.println(Input[i][j]);

    return Input;
}

在此处输入图片说明

i assume that you call the method 'restorePicture' with a simple LinkedList instead an Array of LinkedList. 我假设您使用简单的LinkedList而不是LinkedList数组调用方法“ restorePicture”。 Thats why you get the error. 那就是为什么您得到错误。

Check the method call at line 10 in your code. 检查代码中第10行的方法调用。 Compile error statements in Eclipse are quiet good. 在Eclipse中编译错误语句非常好。

The warning you get because you do not specify the type of LinkedList, so you have to change the parameter definition to. 因为未指定LinkedList的类型而收到警告,所以必须将参数定义更改为。

public static int[][] restorePicture (LinkedList<Integer>[] linked_list) 

To create a new Array of LinkedLIst you have to code 要创建一个新的LinkedLIst数组,您必须编写代码

LinkedList<Integer>[] linked_list = new LinkedList[input.length];

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

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