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

  • SEARCH
  • Home
  • 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

Ask A Question

Stats

  • Questions 545k
  • Answers 545k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • added an answer Well, start off by thinking of which bits of data… May 17, 2026 at 9:17 am
  • added an answer For this task it is a good idea to use… May 17, 2026 at 9:15 am
  • added an answer This is exactly how the Skyhook database (built into many… May 17, 2026 at 9:15 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.