• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cannot assign cloned String array to generic type array.  RSS feed

 
Bob Ivanovich
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code in which I am looping through the rows of one array (composed of Strings) and copying it to another array. I am using .clone() to achieve this and it seems work as it changes the memory location of the rows themselves. I did notice that the String objects are still pointing to the same location in memory in both arrays but I won't worry about that for now, at the moment I just want to understand why the array I am cloning is not successfully assigning to the other array.

This is the incorrect line:

My code:



My compiler says: Required: T[]. Found: java.lang.String[]

Since ar[r] is an array and .clone() also returns an array why is this line incorrect.
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see where you are calling the copyMaze method, but you'll notice that it has a type parameter <T>. You'll need to make sure that you use <String> for that type parameter. If you don't know how to do that (which wouldn't be that unlikely given that you posted in Beginning Java) then show us the code which is calling copyMaze.
 
Bob Ivanovich
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the only place I wanted to use this method is in "mazeCopy" right bellow my maze declaration in the code I posted (It's the line that is commented out). I know it wouldn't make sense to have the parameter "ar" in this case since I can easily access all arrays I need, but I just wanted to test this out.
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems to me that type inference should be working in that case. But you didn't tell us which line of code had the compiler message.
 
Bob Ivanovich
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line specifically:
ar[r] = maze[r].clone();

And also something to note is that if I modify the method and change all <T>, T[][], and modify the parameter types to String no errors occur.
 
Bob Ivanovich
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I mean:


If I change the method like this I get no error.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
clone returns an Object. You can discover this by hovering over the method or pressing Ctrl-Space if you use Eclipse.

Use a cast to fix it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!