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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:47:26+00:00 2026-06-09T05:47:26+00:00

please help me with this. i want to scan array1 through elements of array2

  • 0

please help me with this. i want to scan array1 through elements of array2 and disregard the elements of array2 if it’s found in array1. so a list of fruits will only be left. but the output shows : ball, pencil. where i would want it to display only the elements in fruits. thanks for your help

import java.util.*;
public class CountFruits
{
    public static void main(String args[])throws Exception
    {
        String array1[] = {"apple", "mango", "orange", "banana", "ball", "pencil"};
        String array2[] = {"ball", "pencil"};
        List<String> fruits = new ArrayList<String>();
        for(int x = 0; x < array1.length; x++)
        {
            for(int y = 0; y < array2.length; y++)
            {
                if(array1[x].equals(array2[y]))
                {
                    System.out.println(array1[x] + "\t" + array2[y]);
                    if(!fruits.contains(array1[x]))
                    {
                        fruits.add(array1[x]);
                    }
                }
            }//end for
        }//end for
        System.out.println("fruits: " +fruits);
    }
}
  • 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-06-09T05:47:28+00:00Added an answer on June 9, 2026 at 5:47 am

    The if statement if(array1[x].equals(array2[y])) will only be true for ball and pencil, so the fruit array will only add ball and pencil.
    You could set the fruit array to hold all of array1 and then, if your if statement is true, remove that element from fruit the array.

    or, you could set a boolean isFruit = true (in the outer for loop).
    Then, if your if statement if(array1[x].equals(array2[y])) passes, set isFruit to false.
    After the iteration of the inner loop, if isFruit is true, add it to the fruit array.

    for(int x = 0; x < array1.length; x++)
    {
        boolean isFruit = true;
        for(int y = 0; y < array2.length; y++)
        {
            if(array1[x].equals(array2[y]))
            {
                System.out.println(array1[x] + "\t" + array2[y]);
                isFruit = false;
            }
            if(isFruit)
            {
                fruits.add(array1[x]);
            }
        }//end for loop
    }//end for loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to Integrate Weather radar on my MapView.Please anyone help on doing this
i Want to fetch this data using json decode. Please Help.. So far i
This is my 3rd SO question. Please help again... I want to put up
please help me i am this question again dont block this question i want
please help me this problem. I want to split -action=1 to action and 1.
hi can someone please help me with this issue? i want to send a
Please help me on this. I want to change template page layout of cart
Please help this would be my last problem in dealing with access database with
Please help this beginner here... I have a SQL Server 2008 R2 running on
Please help me with this update query. I COMPLETELY understand how redundant this is

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.