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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:05:25+00:00 2026-05-25T14:05:25+00:00

In Java, suppose I have a method that takes in two lists of integers,

  • 0

In Java, suppose I have a method that takes in two lists of integers, and returns an arrayList of integers from the first list, based upon the second list.

public ArrayList<Integer> func(ArrayList<Integer> A, ArrayList<Integer> B){
   ArrayList result = new ArrayList();

   //A and B have the same length
   for(int index = 0; index < A.length();index++){

      //Decide if a gets added to result based on the corresponding B
      if(decisionFunction(B.get(i)){
          result.add(a.get(i));
      }
   }

 return result;
}

Now suppose I did something silly like change the insertion to

        result.add(b.get(i));

It’d be nice if I could do something like

public ArrayList<AInteger> func(ArrayList<AInteger> A, ArrayList<BInteger> B)

to ensure that regardless of sleep deprivation, the compiler would prevent me from making this type of mistake. Now we could try

class AInteger extends Integer{}; class BInteger extends BInteger{};

and call the method with

func((ArrayList<AInteger>) A, (ArrayList<BInteger>) B);

Unfortunately, downcasting will raise a runtime exception. So, this solution would require me (I think) to rebuild A and B into new Lists with the appropriate datatype, which is expensive. Aside from being more careful, and using unit tests, is there anyway to get the compiler to distinguish between the two lists?

Do other languages such as Scala, or Haskell, provide something that allows me to recast safely without copying?

  • 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-25T14:05:26+00:00Added an answer on May 25, 2026 at 2:05 pm

    By using generics, this can be solved by modifying the function declaration to be

    public <IntegerA extends Integer, IntegerB extends Integer>
       List<IntegerA> func(List<IntegerA> A, List<IntegerB> B){
           ArrayList result = new ArrayList();
    
           //A and B have the same length
           for(int index = 0; index < A.length();index++){
    
               //Decide if a gets added to result based on the corresponding B
               if(decisionFunction(B.get(i)){
                   result.add(A.get(i));
               }
           }
    
           return result;
    }
    

    This ensures that result.add(B.get(i)) raises a type error as desired.

    Unfortunately… new IntegerA(int) is not allowed, which prevents new objects from being created. So resulting exported list can only contain elements from the original list (unless downcasting is performed, which nullifies any type guarantees).

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

Sidebar

Related Questions

Suppose I have a Java method that returns a HashMap object. Because a LinkedHashMap
Suppose that I have a Java class with a static method, like so: class
Suppose I have a Java class that has 100K worth of method code containing
I am using java language,I have a method that is supposed to return an
In Java, suppose I have 3 classes, C extends from B which extends from
I have two questions about java.awt.Shape . Suppose I have two Shape s, shape1
Suppose that I have a Java program within an IDE (Eclipse in this case).
I have a simple question about a java map.Suppose I have a map that
Suppose I have a method to calculate combinations of r items from n items:
Suppose you have a Java class which defines a copyFile(String, String) method: public class

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.