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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:22:10+00:00 2026-05-25T00:22:10+00:00

I have two Arraylists: ArrayList a1 = new ArrayList(); a1.add(5); a1.add(10); a1.add(20); a1.add(50); a1.add(100);

  • 0

I have two Arraylists:

ArrayList a1 = new ArrayList();
a1.add("5");
a1.add("10");
a1.add("20");
a1.add("50");
a1.add("100");
a1.add("500");
a1.add("1000");

ArrayList a2 = new ArrayList();
a2.add("50");
a2.add("500");
a2.add("1000");

How can I compare this two arraylists and add into new arraylist(a3) with 1 if a2 exist in a1 and 0 if not exist, so the result will below for arraylist a3?

a3[0] = 0
a3[1] = 0
a3[2] = 0
a3[3] = 1
a3[4] = 0
a3[5] = 1
a3[6] = 1

Thanks in advance

  • 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-25T00:22:10+00:00Added an answer on May 25, 2026 at 12:22 am

    First, I would advice you to use generics. And secondly, for a2 could be a Set instead. And thirdly you might want to change from String to Integer (since they are all integers).

    But for your example this is a way to do it:

    ArrayList<Integer> a3 = new ArrayList<Integer>();               
    for (String a : a1)
        a3.add(a2.contains(a) ? 1 : 0);
    

    Full example (with a HashSet and Integer type):

    public static void main(String... args) {
        List<Integer> a1 = Arrays.asList(5, 10, 20, 50, 100, 500, 1000);
        Set<Integer>  a2 = new HashSet<Integer>(Arrays.asList(50, 500, 1000));
    
        ArrayList<Integer> a3 = new ArrayList<Integer>();                
    
        for (Integer a : a1)
            a3.add(a2.contains(a) ? 1 : 0);
    
        System.out.println(a3);
    }
    

    Output:

    [0, 0, 0, 1, 0, 1, 1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey, I have this code here: ArrayList arrayList = new ArrayList(); arrayList.add(one); arrayList.add(two); arrayList.add(three);
I have two arraylists, and I would like to have a new arraylist with
I have two arraylist like this ArrayList<Paragens> paragens = new ArrayList<Paragens>(); ArrayList<Rotas> rotas =
I have two array lists dim Colors1 = New ArrayList Colors1.Add(Blue) Colors1.Add(Red) Colors1.Add(Yellow) Colors1.Add(Green)
I have two arrayList ArrayList ar1 = new ArrayList(); ArrayList ar2 = new ArrayList();
Say I have two Collections : Collection< Integer > foo = new ArrayList< Integer
I have an ArrayList : Arraylist<Person> list1 = new ArrayList<Person>(); list1.add(new Person(John, 0)); list1.add(new
So we have a java class with two ArrayLists of generics. It looks like
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
Im developing the java Email application with Timer, I have a two arraylists named

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.