Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrays of different types

Tags:

java

Is it possible to have an array that contains two different types of data? I want to have an array that contains a double and also a string. I attempted:

ArrayList<double><String> array;

But that didn't work.

Sorry for the silly question, but it has been a while since I have used something like this.. Can you refresh my memory on how would I declare and populate such an array?

And then to take it a step further, I would like to sort the array by the double if possible?

Thanks!

like image 229
Tyler Avatar asked May 12 '10 01:05

Tyler


People also ask

Can you have arrays of different types?

You can create an array with elements of different data types when declare the array as Object. Since System. Object is the base class of all other types, an item in an array of Objects can have a reference to any other type of object.

How do you create an array of different types?

To create an array type you can use Array<Type> type where Type is the type of elements in the array. For example, to create a type for an array of numbers you use Array<number> . You can put any type within Array<Type> .

What is an array different types of arrays?

Arrays are classified into two types based on their dimensions : single-dimensional and multi-dimensional. Logically, a single-dimensional array represents a linear collection of data, and a two-dimensional array represents a mathematical matrix. Similarly, a multidimensional array has multiple dimensions.

What are the different types array operations?

Basic OperationsTraverse − print all the array elements one by one. Insertion − Adds an element at the given index. Deletion − Deletes an element at the given index. Search − Searches an element using the given index or by the value.


2 Answers

Firstly, it's worth being clear about the difference between an array and an ArrayList - they're not the same thing at all.

However, in either case you can't do what you want. The closest you can probably come is declaring your own type. (EDIT: My original code had a double or a string... I've now changed it to be a double and a string. Let me know if this change isn't what you had in mind.)

public final class DoubleAndString
{
    private final String stringValue;
    private final double doubleValue;

    public DoubleAndString(String stringValue, double doubleValue)
    {
        this.stringValue = stringValue;
        this.doubleValue = doubleValue;
    }

    public String getString()
    {
        return stringValue;
    }

    public String getDouble()
    {
        return doubleValue;
    }
}

Then create an ArrayList<DoubleAndString> or a DoubleAndString[].

Now, this feels somewhat vanilla at the moment - presumably the double and string values actually have a greater meaning - a name and a score, for example. If so, encapsulate that in a type which describes the pairing more appropriately.

As for ordering - you could make DoubleAndString implement Comparable<DoubleAndString> - but unless that's the only natural ordering which makes sense, I'd write a Comparator<DoubleAndString>:

public class DoubleComparator implements Comparator<DoubleAndString>
{
    public int compare(DoubleAndString ds1, DoubleAndString ds2)
    {
        return Double.compare(ds1.getDouble(), ds2.getDouble());
    }
}

Then you can use Collections.sort to sort an ArrayList<DoubleAndString> or Arrays.sort to sort an array.

like image 61
Jon Skeet Avatar answered Nov 10 '22 05:11

Jon Skeet


You can use ArrayList<Object> and you can then use anything you'd like. Encapsulate the double in a Double object and when you retrieve the object use instanceof to check if it's really a double or a String.

I must say, it's unlikely this 'design' would win you any awards. Is it possible to rethink the solution you're considering for your problem, and see if you could do with a different kind of approach?

like image 37
roguesys Avatar answered Nov 10 '22 07:11

roguesys