0

I have a list of lists in Java which has 3 lists of integers in it.

List<List<Integer>> L;
         myList = new ArrayList<>();
         for(int i=0; i<3; i++){
                myList.add(new ArrayList<Integer>());
         }
myList.get(0).add(1);
myList.get(0).add(2);
myList.get(0).add(3);
myList.get(0).add(4);
myList.get(1).add(1);
myList.get(1).add(2);
.
.
.

My problem is that the number of the items that can be added to each of lists is unlimited in my code, but I want to limit the number of the items that each list can get. For example, each list can get only 4 items and when the 5th item is received by one of these lists, I need a shift to the left so the 5th item can be added to the list. Here is an example of what I intend to do:

At first: myList = [[1,2,3,4],[1,2,3,4],[1,2,3,4]]

After the 5th item is added to the first list of myList: myList = [[2,3,4,5],[1,2,3,4],[1,2,3,4]]

I tried to limit the number by something like this:

for(int i=0; i<3; i++){
        myList.add(new ArrayList<Integer>(4));
 }

but it didn't work. How can I do this?

helen
  • 587
  • 1
  • 10
  • 26
  • 2
    So, shat have you tried? What's the concrete problem you're facing? – JB Nizet Nov 03 '18 at 22:34
  • Not quite the same, but basically the same topic only for [queues](https://stackoverflow.com/questions/1963806/is-there-a-fixed-sized-queue-which-removes-excessive-elements) –  Nov 03 '18 at 22:38
  • If you need the item limit to be applied automatically, then you'll need to create a custom `List` implementation for your inner lists. This is relatively easy to do by overriding `AbstractList`. Do note that the result, although it implements `List`, will not correctly satisfy the contract of that interface. – John Bollinger Nov 03 '18 at 22:39
  • @JBNizet I'm new to Java. I wasn't sure about how I could do this. – helen Nov 03 '18 at 22:45
  • You're supposed to do some research, experiments, attempts, tests before asking a question. You'll do that for your whole career. Read the docuentation of List, and try doing it. It seems pretty straightforward to implement, doesn't it? – JB Nizet Nov 03 '18 at 22:46
  • @JBNizet I edited my post and added what I had done to solve the problem. – helen Nov 03 '18 at 22:53
  • What you posted just adds 3 more empty lists to your outer list. You need a method that gets an inner list by its index, check if its size is 4, if it is, remove its first element, and then adds the new value to it. – JB Nizet Nov 03 '18 at 22:55
  • @JBNizet Thank you, I got it. – helen Nov 03 '18 at 23:01

2 Answers2

1

1st method: Create a custom List/ArrayList whatever you want.

public class CustomArrayList extends ArrayList<Object> {
  @Override
  public boolean add(Object e) {
      if (this.size() < yourLimitSize) {
          return super.add(e);
      }
     // shift
      return false;
  }
}

2nd method:

wrap your list with:

if(list!= null && list.size() < yourLimitSize(){
list.add(yourElement)
}else{
  //  shift
}
Sz-Nika Janos
  • 811
  • 6
  • 19
  • 1
    The second method is the right one. A List impelmentation should adhere to the List contract specified in the documentation. And it would fail is somene called addAll(), for example. It should be encapsulated into a dedicated FifoBuffer class, which is not a List. – JB Nizet Nov 03 '18 at 22:42
  • For approach 1, be sure to also get the other overload of `add`, as well as the two overloads of `addAll`. (Potential bugs like that are why I would favor approach 2, as JB Nizet said.) – yshavit Nov 03 '18 at 22:42
0

In this particular case it's more convenient to use Queue and poll in order to poll always the first element from the list if the number of elements is under the LIMIT

       final int LIMIT = 4;

        List<Queue<Integer>>
        myList = new ArrayList<>();
        for(int i=0; i<3; i++){
            myList.add(new LinkedList<Integer>());
        }
        myList.get(0).add(1);
        myList.get(0).add(2);
        myList.get(0).add(3);
        myList.get(0).add(4);
        myList.get(1).add(1);
        myList.get(1).add(2);


        System.out.println(myList);

        myList.get(0).add(5);
        if(myList.get(0).size() > LIMIT){
            myList.get(0).poll();
        }

        System.out.println(myList);
    }

Output:

[[1, 2, 3, 4], [1, 2], []]
[[2, 3, 4, 5], [1, 2], []]
gkatiforis
  • 1,588
  • 9
  • 19