0

My Code is as follows:

ArrayList<Integer> al = new ArrayList();
al.add(1);
al.add(2);
al.add(3);
al.add(3);
al.add(4);

I want the following pattern during printing:

[1,2,4]

I tried both for loop and Iterator but I am not getting the desired output.

Help me to sort out!

I found the solution and that is

for(int i=0;i<al.size();i++) {
if(al.get(i)!=3) 
   System.out.println(al.get(i));
  }

thanks for your help

Now I want to get the same output, not through printing but by deleting elements of ArrayList, I tried with the same condition but I got exception

and the answer is

Iterator<Integer> iter = al.iterator();
        while (iter.hasNext()) {
            if (iter.next().intValue() == 3) {
                iter.remove();
            }
        }
        System.out.println(al);
Sudip7
  • 2,384
  • 3
  • 27
  • 35

4 Answers4

4

Step through all the items in the ArrayList and test if they are equal to 3, if they are not, print them.

for (Integer i : al)  //for each Integer in the al list
{
  if (!i.equals(3))  //if it is NOT (!) equal to 3
  {
    System.out.println(i);  //then print it
  }
}

Obviously if you want to skip more than just the number 3 you will need to expand the condition the if uses.

Note on removing elements from the list

If you try and remove from the list within the loop using this method you will encounter problems with ConcurrentAccessException, this is dealt with in this question.

Community
  • 1
  • 1
Ross Drew
  • 8,163
  • 2
  • 41
  • 53
  • Now I want to get the same output, not through printing but by deleting elements of ArrayList, I tried with the same condition but I got exception – Sudip7 Feb 27 '14 at 13:59
  • You need to provide more info but most likely a `ConcurrentAccessException` if you are trying to delete items in a for each loop. This means using the normal `for` loop i.e. `for (x;y;z)` or http://stackoverflow.com/questions/18547584/java-concurrentmodificationexception-foreachenhanced-loop-single-thread – Ross Drew Feb 27 '14 at 14:35
  • Good, I've expanded my answer as well. – Ross Drew Feb 28 '14 at 14:59
0

If you want to get the list element and as per your output it seems you want to print 1 and even numbers in the list

for(int i=0; i< a1.size(); i++)  //iterate over List
{
    if(a1.get(i)%2=0 || a1.get(i) == 1) //print if 1 or even
      System.out.println(a1.get(i));
}
eatSleepCode
  • 4,427
  • 7
  • 44
  • 93
0

You can also use the forEach method (Java 8):

al.forEach(v -> {if(v!=3) System.out.println(v);});
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
0

add the required skipping values in a list and check against that list before printing

My code:run and give comment(views please)

import java.util.*;
public class Mycode{

 public static void main(String []args){
     ArrayList<Integer> al = new ArrayList();
al.add(1);
al.add(2);
al.add(3);
al.add(3);
al.add(4);

      ArrayList<Integer> al1 = new ArrayList();
      al1.add(3);
      al1.add(2);
      int flag=0;
      for(int j=0; j< al.size(); j++)
      {
          flag=0;
      for(int i=0; i< al1.size(); i++)
      {
      if(al.get(j)==al1.get(i))
      flag=1;
      }
        if(flag==0)
      System.out.println(al.get(j));
      }
 }
}