Question

How to complete this JAVA method? public static <E> AttachedList<AttachedList<E>> pack(AttachedList<E> flatList) { //given a 1D...

How to complete this JAVA method?
public static <E> AttachedList<AttachedList<E>> pack(AttachedList<E> flatList) {
   //given a 1D (flatList), "pack" sequential items together
   //to form a 2D list of values

   //Example 1: [1,1,2,3,3] becomes [[1,1],[2],[3,3]]
   //Example 1: [1,1,2,1,1,2,2,2,2] becomes [[1,1],[2],[1,1],[2,2,2,2]]
   //Example 3: [1,2,3,4,5] becomes [[1],[2],[3],[4],[5]]
   //IMPORTANT: the above examples are _lists NOT arrays_

   //promise: we will never test this with nulls in the flatList
   //though there's no harm in coding it to work with nulls
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
public static <E> AttachedList<AttachedList<E>> pack(AttachedList<E> flatList) {

   AttachedList<AttachedList<E>> result = new AttachedList<>();
   E lastItem;
   AttachedList<E> lastList = new AttachedList<>();
   
   
   for(int i=0; i<flatList.size(); i++) {
                E item = flatList.get(i);
                
                if(i == 0) {
                        lastItem = item;
                        lastList.add(item);
                } else {
                        if(item.equals(lastItem)) {
                                lastList.add(item);
                        } else {
                                result.add(lastList);
                                lastList = new AttachedList<>();
                                lastList.add(item);
                        }
                        lastItem = item;
                }               
   }
   
   if(lastList.size() != 0) {
                result.add(lastList);
   }
   return result;
}
**************************************************
You have not given the code for AttachedList class, So i can not really test the code.. 
Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
How to complete this JAVA method? public static <E> AttachedList<AttachedList<E>> pack(AttachedList<E> flatList) { //given a 1D...
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