Java: matrix data type for inserting values ​​based on their coordinates

I have a requirement in which I need to read the values ​​and their coordinates and put them in a matrix to display it later.

so let's say i have the following:
<name='abc', coordinates='1,3'>
<name='xyz', coordinates='2,1'>
...............................

Now I need to put them in the "matrix collection" based on their coordinate values ​​and get the display in the form of a table (with cells in the table occupying the corresponding slot for the coordinates).

Is there an assembly / way to do this in java? Keep in mind, I don't need a swing or any graphics library methods. I just need a data structure to do this.

Thanks BC

+4
source share
3 answers

You can use the class table from Guava.

+4
source

If you know the boundaries of your grid in advance, you can use a 2-dimensional array:

 int[][] matrix = new int [n][n]; 

If you do not, one way to emulate this is to list the lists:

 ArrayList <ArrayList<Integer> > matrix = new ArrayList <ArrayList <Integer> >(); 
+1
source

Nothing will do this automatically for you AFAIK. You will need to start by extracting the data. Depending on how it is provided to you, you can use regular expressions or some specialized parser (if it is XML, there is a wide range of Java tools there).

Next, you will need to separate this String coordinate. Check the split method of the String class.

Finally, these coordinates must become integer. Check the parseInt method of the Integer class.

Using these numeric coordinates, you can insert a value into an array. If you know the maximum coordinates in advance, you can immediately create an array. If the coordinates can be any values ​​without restrictions, you will need some kind of dynamic structure or regularly create a larger array and copy over the old contents.

+1
source

All Articles