0

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. 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). 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;
}

enter image description here

Khaled.K
5,9801 gold badge35 silver badges52 bronze badges
asked Dec 17, 2015 at 7:09
8
  • Why not show us the entire error? Also, indent your code Commented Dec 17, 2015 at 7:10
  • 1
    Please format the code. Commented Dec 17, 2015 at 7:11
  • 1
    Is there a class declaration? Something like "public class Test {" before the functions? Commented Dec 17, 2015 at 7:11
  • 1
    public static int[][] restorePicture (LinkedList<Integer>[] linked_list) Commented Dec 17, 2015 at 7:32
  • 1
    Now try removing the parentheses around linked_list.length in the 2d int array. Commented Dec 17, 2015 at 7:37

1 Answer 1

3

i assume that you call the method 'restorePicture' with a simple LinkedList instead an Array of LinkedList. Thats why you get the error.

Check the method call at line 10 in your code. Compile error statements in Eclipse are quiet good.

The warning you get because you do not specify the type of LinkedList, so you have to change the parameter definition to.

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

To create a new Array of LinkedLIst you have to code

LinkedList<Integer>[] linked_list = new LinkedList[input.length];
answered Dec 17, 2015 at 7:40

6 Comments

I don't have any more errors, just a warning now. I'm not quite sure what you mean by what you said
I think the OP code will always throw OutOfBoundsException, because in the inner loop he does j++ twice
Thanks man, a last small question, if I were to initialize an array of a linked list for example: LinkedList linked_list<Integer>[] = new LinkedList[input.length]<Integer>(); what would be the correct way to write it because that is wrong, thanks
@Boris @SuppressWarnings({"unchecked"}) LinkedList<Integer>[] list = new LinkedList[length]; is the correct way. Welcome to java generics.
@Boris The sentence "welcome to java generics" points to java's type erasure, which is a failure in design - your question is fine.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.