Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple type array

Tags:

java

arrays

I am working with an array and need some help. I would like to create an array where the first field is a String type and the second field is an Integer type. For result:

Console out

a  1
b  2
c  3
like image 566
Pirr Avatar asked Apr 04 '12 08:04

Pirr


People also ask

Can you have multiple types in an array?

Arrays that contain instances of multiple types are often referred to as heterogenous arrays, and are incredibly commonly used in dynamically typed languages — like PHP, JavaScript, and Ruby.

How do I create an array with multiple types?

Use a union type to define an array with multiple types in TypeScript, e.g. const arr: (string | number)[] = ['a', 1] . A union type is formed from two or more other types. The array in the example can only contain values of type string and number . Copied!

Can you have an array with multiple types Java?

An array can only have a single type. You can create a new class like: Class Foo{ String f1; Integer f2; } Foo[] array=new Foo[10]; You might also be interested in using a map (it seems to me like you're trying to map strings to ids).

How many types of array are there?

Types of arrays? There are two types of array: Two-dimensional array. Multi-dimensional array.


1 Answers

An array can only have a single type. You can create a new class like:

Class Foo{
   String f1;
   Integer f2;
}

Foo[] array=new Foo[10];

You might also be interested in using a map (it seems to me like you're trying to map strings to ids).

EDIT: You could also define your array of type Object but that's something i'd usually avoid.

like image 92
Filip Avatar answered Oct 12 '22 01:10

Filip