Question

Write a method that given a non-empty ArrayList of Integers parameterand also an Integer parameter, the...

Write a method that given a non-empty ArrayList of Integers parameterand also an Integer parameter, the method returns a new ArrayList containing the elements from the original ArrayList that come after the first occurrence of Integer parameter in the original ArrayList.

If the original ArrayList does not contain an occurrence of the Integer parameter return an empty ArrayList . The original ArrayList must be unaffected by the method execution.

Following are some examples of the invocation of the method using explicit parameters, with an ArrayList represented by a bracketed list. The returned ArrayList follows the '→' character.

postList({1, 2, 4, 1}, 4);  →  {1}
postList({3, 1, 4}, 3);     →  {1, 4}
postList({1, 4, 4}, 4);     →  {4}
postList({1, 2, 4, 1}, 5);  →  {}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.ArrayList;
import java.util.List;

public class ArrayListEx {
   public static void main(String[] args) {
       ArrayList<Integer> list = new ArrayList<>();
       list.add(3);
       list.add(1);
       list.add(4);
       list.add(5);

       System.out.println(postList(list, 3));
   }

   private static List postList(ArrayList<Integer> l, int n) {
       ArrayList<Integer> list = new ArrayList<>();
       boolean flag = false;
       for (int x : l) {
           if (x == n) {
               flag = true;
               continue;
           }
           if (flag)
               list.add(x);

       }
       return list;
   }
}

LITTITULLU TUyLISLL [1, 4, 5]

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
Write a method that given a non-empty ArrayList of Integers parameterand also an Integer parameter, the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT