Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two-dimensional array of different types

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?

like image 565
lunar Avatar asked Apr 08 '12 13:04

lunar


People also ask

Can you have a 2D array with different types?

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.

How many data types can a two-dimensional array have?

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.

Can a 2D array have different data types in C?

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.

What are two-dimensional arrays?

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.


1 Answers

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

like image 156
amit Avatar answered Oct 03 '22 17:10

amit