1

I am trying to create an ArrayList of Object[] from an Object[][] perform some changes and then getting a new Object[][] with the changes made.

However I get this compile error:

error: incompatible types: Object[] cannot be converted to Object[][]

when performing this:

protected Object[][] data;
fillData();
ArrayList dlist= new ArrayList<Object[]>( Arrays.asList(data) ); //NO COMPILE ERROR
Object[] example=new Object[2]; //just to use in toArray()

String [] args= {"new data 1","new data 2"};//yeah just a typo thanks Oscar
dlist.add( args ); //Sending String instead of Object is legal thanks Oscar.

data= dlist.toArray( example ); //COMPILER ERROR incompatible...

I am using the wrong statement with no compiler error: ArrayList dlist= new ArrayList<Object[]>( Arrays.asList(data) ); because the output is Object[] instead of Object[][] which is what i want Thanks for your help.

3
  • List<Object[]> dlist= new ArrayList<Object[]>( Arrays.asList(data) ); compiles for me. What's your exact problem? Probably you didn't add the necessary imports or something. Commented Aug 11, 2014 at 0:54
  • dlist.add(<whatever>) works because you have declared ArrayList dlist which is raw, similar to declare ArrayList<Object> dlist which means you can store anything there. Commented Aug 11, 2014 at 0:55
  • What do you mean by I am using the wrong command, thanks for your help? Is your problem solved or what? Commented Aug 11, 2014 at 1:00

2 Answers 2

1

I tried this piece of code and works:

Object[][] data = { { "hello", "world" }, { "bye", "world" } };
//convert Object[][] to List<Object[]>
List<Object[]> dlist = new ArrayList<>(Arrays.asList(data) );
for (Object[] o : dlist) {
    System.out.println(Arrays.deepToString(o));
}
//convert List<Object[]> to Object[][]
Object[][] data2 = dlist.toArray(new Object[0][]);
System.out.println(Arrays.deepToString(data2));

Also, Java language doesn't work with commands, except if you refer to the command line to compile your files, but that's outside the Java programming language.

Sign up to request clarification or add additional context in comments.

7 Comments

Object[][] data2 = dlist.toArray(new Object[dlist.size()][]); would avoid creating unnecessary garbage as well.
Object[][] data = dlist.toArray(new Object[dlist.size()][]); would be enough? i don't want to keep data "data". just the new. And Luiggi Object[][] data2 = dlist.toArray(new Object[0][]); should I put this in an iterator and replace 0 by a counter? or your statement is updating the whole two dimensional array?
@lrn2java I'm replacing the whole Object[][] 2d array.
@msandiford I leave that up to the JVM.
Okey thanks estimado, I will continue my little proyect and then do the reading on arrays and Lists, the deepToString seems to do the trick.
|
0

Do it like this:

List<Object[]> dlist = new ArrayList<Object[]>();
for(Object[] i: data) {
    dlist.add(i);
}

7 Comments

This is what OP is trying to avoid.
Can't see where in the question did he specify that he was trying to avoid this. Also, why?
Usage of Arrays#asList will suffice.
Can't see how ArrayList<Object[]> dlist = new ArrayList<Object[]>(Arrays.asList(objects)); doesn't compile though... it works on my machine.
That's the problem, it is an unclear question and we don't know the exact problem OP has.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.