Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 784343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:41:53+00:00 2026-05-14T20:41:53+00:00

Is it possible to have an array that contains two different types of data?

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:41:54+00:00Added an answer on May 14, 2026 at 8:41 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a tab-delimited text file that contains data arranged in columns
I have a parent class that contains an array of objects each with a
Is there any way to create a multidinensional array that contains arrays of different
Is it possible to have a different set of dependencies in a maven pom.xml
I want to use two independent $_SESSIONs in a single PHP script. I have
Hey all, I have an array that holds all of the .aif file paths
Is it possible to have selective queries in PostgreSQL which select different tables/columns based
I am working on an object that contains an array of queues with an
Is it possible to have a MySQLi prepared statement within the fetch() call of
Is it possible to have XML-embedded JavaScript executed to assist in client-side (browser-based) XSL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.