Tokuchi Toua
Tokuchi Toua

Reputation: 175

comparing elements of array

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);
    }
}

Upvotes: 1

Views: 414

Answers (5)

J_Goodman
J_Goodman

Reputation: 36

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

Upvotes: 1

Pranav Kevadiya
Pranav Kevadiya

Reputation: 539

There are two flaws in your logic

1)Here You are adding the elements which should not be added in fruits list. So you will have to check the elements which are not part of array2 and those should be added.So you may write that logic in else part.

2)You are checking each time whether the element already present in fruits list,instead of that use Set.It will disallow duplicate entries

Upvotes: 1

J_Goodman
J_Goodman

Reputation: 36

The best approach would be, if you are using an IDE, step through the code, see where the logic fails. If you are not using a debugger, then put some System.out.println statements into the code. Then you can determine where the logic fails. The other solution is to attempt the first solution I gave. That is, have the fruit array hold all of array1 and then remove elements that are found in array2

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727067

First, I am assuming that this is a homework of sorts, so I wouldn't spoil your exercise by fixing your code.

Here is the idea: you need to create a boolean variable that indicates if the nested loop has found anything or not, set it to "not found" going into the loop, and checking it after the loop. If the loop indicates that an item has been found, skip it; otherwise, add it. Remember, you can do it only after the nested loop has finished, so calling fruits.add inside the nested loop is premature.

boolean found = false;
for(int y = 0; !found && y < array2.length; y++) {
    found |= array1[x].equals(array2[y]);
}
if (!found) ...

Upvotes: 3

assylias
assylias

Reputation: 328913

You need to add the array1[x] to your list if it is NOT in array2[x]:

if (array1[x].equals(array2[y])) {
    System.out.println(array1[x] + "\t" + array2[y]);
} else {
    fruits.add(array1[x]);
}

But you will have duplicates. You can add a condition if(!fruits.contains(...)) before adding.

You could simplify your code by using the following algorithm:

  • add all the items in array1 to a list
  • remove the items in array2 from the list

Upvotes: 2

Related Questions