I want to create a two-dimensional array in which I want to store records from the database. So lets say that the first is of type int
and the second of type String
(here I am describing just one record so basically types of db columns). How can I do it? Is an array the right data structure for that?
You can have multiple datatypes; String, double, int, and other object types within a single element of the arrray, ie objArray[0] can contain as many different data types as you need. Using a 2-D array has absolutely no affect on the output, but how the data is allocated.
Arrays can only contain one type. If that type happens to be Object then it can store Object and any of its sub-types, but that doesn't really sound like what you're trying to accomplish here.
In C, you cannot declare an array, two-dimensional or otherwise, such that some parts of the array are of one type and some are of another type.
A two-dimensional array is similar to a one-dimensional array, but it can be visualised as a grid (or table) with rows and columns. Many games use two dimensional arrays to plot the visual environment of a game.
I am not sure I am following, but you might be looking for a Map<Integer,String>
. or Map<Integer,List<String>>
. [have a look on List, and HashMap]
Map
allows association of the key [Integer
] to the value [String
or List
].
Map
also allows fast lookup of key, and its attached value.
(*) You should use Map<Integer,List<String>>
if you want to attach more then one String
per Integer
, or alternatively you can use apache commons MultiMap
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With