JavaEar 专注于收集分享传播有价值的技术资料

CircularFifoQueue doesn't work inside the method, what to use instead?

I need to write the method which let to store always last 10 (the newset) elements and only 10.I have tried to use CircularFifoBuffer.It works perfectly usee like this:

 import java.util.ArrayList;
 import java.util.List;
 import java.util.Queue;
 import org.apache.commons.collections4.queue.CircularFifoQueue;

 public class Main {

public static void main(String[] args) {

    Queue<Integer> fifo = new CircularFifoQueue<Integer>(3);
    fifo.add(11);
    fifo.add(22);
    fifo.add(33);
    fifo.add(44);
    fifo.add(55);
    System.out.println(fifo); // [33, 44, 55]

But it doesn;t work when used inside the method:

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import org.apache.commons.collections4.queue.CircularFifoQueue;

public class TV {

   public int channelNumber = 11;

   public int getChannelNumber() {
      return channelNumber;
   }

   public void addToChannelsHistory(int channnelNumber) {
      Queue<Integer> fifo = new CircularFifoQueue<Integer>(3);
      fifo.add(channnelNumber);
      System.out.print(fifo);
   }
}

Could you help what to use instead?

2个回答

    最佳答案
  1. You have to use the notion of attribute, a member of your class which is a data, not a method:

    public class TV {
    
       private final Queue<Integer> fifo = new CircularFifoQueue<Integer>(3);
    
       public Queue<Integer> getChannelNumbers() {
          return fifo;
       }
    
       public Integer getChannelNumber() {
          return fifo.isEmpty() ? null : fifo.peek();
       }
    
       public void addToChannelsHistory(int channnelNumber) {
          fifo.add(channnelNumber);
       }
    
       public String toString() {
          return fifo.toString();
       }
    
       public static void main( String[] args ) {
          TV tv = new TV();
          tv.addToChannelsHistory(11);
          tv.addToChannelsHistory(22);
          tv.addToChannelsHistory(33);
          tv.addToChannelsHistory(44);
          tv.addToChannelsHistory(55);
          System.out.print( tv );
       }
    }
    
  2. 参考答案2
  3. Forgive me if I've misunderstood, but as far as I can tell by copying this locally, this works. However, in a Java program, the main method is the entry point into the program. If you aren't instantiating your TV class in the main method, the addToChannelHistory method will never get run. For instance, this works for me:

    public class TV {
    
        public int channelNumber = 11;
    
        public int getChannelNumber() {
            return channelNumber;
        }
    
        public void addToChannelsHistory(int channnelNumber) {
    
            Queue<Integer> fifo = new CircularFifoQueue<Integer>(3);
            fifo.add(channnelNumber);
    
            System.out.print(fifo);
        }
    
        public static void main(String[] args) {
            TV tv = new TV();
            tv.addToChannelsHistory(11);
            tv.addToChannelsHistory(22);
            tv.addToChannelsHistory(33);
            tv.addToChannelsHistory(44);
            tv.addToChannelsHistory(55);
        }
    }
    

    Running that program should print out 33 to the console.