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

Related Questions

Possible Duplicate: How can you combine two arrays? I have variable $data1 that contains
I have a struct-array that contains details of different reports that can be run.
If I have a variant array which holds nothing but simple types, and possible
Let's say I have a function that takes a string. That string contains the
I have code that searches a folder that contains SQL patch files. I want
Problem: I have two arrays that can possibly be different lengths. I need to
I have a byte array that contains a jpeg image. I was just wondering
I'd like to have an array in javascript that each item contains 2 properties
Let's say I have a tab-delimited text file that contains data arranged in columns
I have a record that contains a dynamic array. It is normal that when

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.