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 483991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:15:13+00:00 2026-05-13T01:15:13+00:00

Suppose that I am collecting n amount of Strings into an array, or an

  • 0

Suppose that I am collecting n amount of Strings into an array, or an array list, or some other data structure. I want to come up with a design that will allow the user to specify how the Strings will be sorted:

-If the user specifies option A, then the Strings will be put into a Vector, and then sorted and printed.

-If the user specifies option B, then the Strings will be put into a List, and then sorted and printed.

I want my design flexible enough to allow me to easily add additional sorting methods in the future (and my sorting methods).

My current implementation involves collecting the Strings from the user into an ArrayList, and then depending on the specified sorting method, I will use Collections.copy(array, vector) or Collections.copy(array, list). Then, I will do Collections.sort(vector or list).

(Currently, I am receiving NullPointerExceptions on Collections.copy)…

I am unsure of whether I am going about it in the best, most re-usable way. Any thoughts?

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-13T01:15:14+00:00Added an answer on May 13, 2026 at 1:15 am

    You need to use the interfaces that Java provides. When you take in an ArrayList you are forcing an implementation upon the user. Take in a Collection<String> and they can use whatever they want.

    If you wanted to force them in to a List implementation you would simply specify List<String> as the parameter. Then they can use a Vector, ArrayList, LinkedList, etc.

    In order to achieve the sorting you will simply want to pass in a Comparator and call Collections.sort(myList, myComparator);

    So you would end up with something like …

    public List<String> sortThem(List<String> strings, Comparator<String> comp) {
        ... do cool things ...
    
        Collections.sort(strings, comp);
    
        return strings;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a scraper that is collecting some data from elsewhere that I have
Suppose I have a Collection of some kind that itself contains Collections; e.g., Dictionary(Of
Suppose that a group wants to encrypt some information, then share the encryption key
Suppose that you have two huge files (several GB) that you want to concatenate
Suppose that I have two data types Foo and Bar. Foo has fields x
Suppose I have a custom collection class that provides some internal thread synchronization. For
I'm trying to write a Powershell function that takes an array argument. I want
Suppose I have a solution inside a TFS collection that contains 3 projects. Ho
In the vein of programming questions : suppose there's a collection of objects that
Suppose that a table named SuplProd has the columns supplier and product and two

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.