0

I'm building e-commerce application and i have one case scenario where i need to cross out sizes which are not available in store.

I have fixed arraylist of strings with sample like this: "36", "38", "40", "42" and i have available sizes with sample like this: "36", "38", "40"

Now i need to iterate through first array and cross out those sizes which are not available.

Here is one part of code where i'm doing that:

// tempSizes - available sizes
// mProduct.getSizes() - all sizes
for (String tempSize : tempSizes) {
      for (int i = 0; i < mProduct.getSizes().size(); i++) {
            if (tempSize.equals(mProduct.getSizes().get(i))) {
                 // if size is available
                 sizes.add(new Size(mProduct.getSizes().get(i), true));
            } else {
                 // if size is not available
                 sizes.add(new Size(mProduct.getSizes().get(i), false));
            }
      }
}

Problem here is that nested for loop will be called three times and the result will output with duplicates of sample. If there is an easier way to do this, please let me know, i would appreciate it.

5
  • These aren't arrays. They might be ArrayLists, though. Commented May 28, 2017 at 14:16
  • they seem like list/arraylist api Commented May 28, 2017 at 14:18
  • Pardon, it's an array list. Commented May 28, 2017 at 14:19
  • "the result will be three arraylist with one available size" I don't understand: you don't create 3 arraylists here, you add everything into one. Commented May 28, 2017 at 14:24
  • If sample is 36, 38, 40 and available sizes are 36, 38 the output will be 36(available), 38, 40, 36, 38(available), 40 and it should be something like this: 36(available), 38(available), 40 Commented May 28, 2017 at 14:27

2 Answers 2

3

You can easily do it with ArrayList using one for loop, e.g.:

List<Integer> samples = Arrays.asList(36, 38, 40 ,42);
List<Integer> available = Arrays.asList(36, 38, 40);
List<Integer> unavailable = new ArrayList<>();
for(int size : samples){
    if(!available.contains(size)){
        unavailable.add(size);
    }
}
System.out.println(unavailable);

This will iterate through all the samples, check whether they are available and if not, put them into anoter list. If you have arrays, you can use Arrays.asList() method to convert them into the Lists.

Sign up to request clarification or add additional context in comments.

5 Comments

unavailable.addAll(available); unavailable.removeAll(samples);.
@AndyTurner I would rather not change the existing lists/arrays as they might get used further down the line. I agree with the approach though.
@DusanDimitrijevic you can accept the answer if it has solved the problem.
@DarshanMehta "I'd rather not change the existing lists" This changes the existing lists no more or less than your approach.
@AndyTurner Ah okay, I thought it was samples.removeAll(available), please ignore my previous comment.
1

Try this code

// mProduct.getSizes() - all sizes
for (int i = 0; i < mProduct.getSizes().size(); i++) {


    int prodSize = mProduct.getSizes().get(i);      
    boolean sizeFound = false;

    // tempSizes - available sizes
    for (String tempSize : tempSizes) {

            if (tempSize.equals(mProduct.getSizes().get(i))) {
                 // if size is available
                 sizes.add(new Size(prodSize, true));
                 sizeFound = true;
                 break;

            }

      }

      if(sizeFound == false){
           // if size is not available
           sizes.add(new Size(mProduct.getSizes().get(i), false));
      }

}

2 Comments

Both answers are helpful, but Darshan's approach i think is a little better because it doesn't requires nested for loop and there is less code.
Yes, agree with you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.