aspose file tools*
The moose likes Java in General and the fly likes I need to convert 2d array in List or arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "I need to convert 2d array in List or arraylist" Watch "I need to convert 2d array in List or arraylist" New topic
Author

I need to convert 2d array in List or arraylist

Jame Sutton
Greenhorn

Joined: Jul 23, 2014
Posts: 10
Hi,i need to convert this 2d array:



in a list or a arraylist and I need to know if it will work in a abstract class...I am adding number with Sudoku.board[Case.getLigne()-1][Case.getColone()-1]=Case.getValeur(); but I need to be able to remove (the last 2 results entered on the board) some number from the board but it didn't seem to work with array to I want to try by convert it in a list or arraylist...Thank!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
What 2D array? There ain't no such thing in Java. What you have shown is an array of arrays, which is better than a 2D array.
I am pretty sure that the presence of the keyword static there is a mistake. I suspect you want that to be an instance field.
If you go through the Arrays class you will find methods called toList or asList or similar. Those might help turning the arrays into a List. Beware: you may not be able to modify the Lists returned from those methods.

By the way: if you declare and initialise that array in the same line you may miss out new int[][] but you still have to write the rest of the initialisation. Put the [] as part of the type not part of the name. The type is int[][], so it would be better style to move the [][] earlier in that statement.
Jame Sutton
Greenhorn

Joined: Jul 23, 2014
Posts: 10
How would you wrote that? Just that:static int board[][]= ?
Jame Sutton
Greenhorn

Joined: Jul 23, 2014
Posts: 10
I would like to do somthing like this : ArrayList<board> arrayList = new ArrayList<board>(Arrays.asList(array)); but it don't seem to work...I don't know if It can work cause my class is abstract and it need to be astract...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
That sounds as though you were guessing. Please explain exactly how you would turn that array into a List and what you would do with it.

Also explain why you have marked it static.
Jame Sutton
Greenhorn

Joined: Jul 23, 2014
Posts: 10
I want to be able to place number on the board I showed in my first post than be able to use remove/size to delete the last 2 numbers entered on the board...Also I wrote it static because all my program was in static ...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
If it is a mistake to make the array static, it is probably also a mistake to make the whole program static.
You are supposed to be writing object‑oriented code, and things static are (usually) not object‑oriented.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I need to convert 2d array in List or arraylist