자바의 FIFO 클래스
Java의 클래스를 통해 FIFO를 구현하고 싶습니다.
그러한 클래스가 이미 존재합니까? 그렇지 않은 경우 어떻게 직접 구현할 수 있습니까?
노트
http://www.dcache.org/manuals/cells/docs/api/dmg/util/Fifo.html 여기에서 클래스를 찾았 지만 dmg.util. *이 포함되어 있지 않습니다. 그러한 패키지가 존재하는지 모르겠습니다.
당신은 어떤 구현하는 클래스를 찾고 대기열 인터페이스를 제외 PriorityQueue
하고 PriorityBlockingQueue
는 FIFO 알고리즘을 사용하지 마십시오.
아마도 (끝에 하나를 추가) 및 (앞에서 하나를 제거하고 반환 )을 사용 하는 LinkedList가 사용 하기 가장 쉬운 것입니다.add
removeFirst
예를 들어, 다음은 LinkedList를 사용하여 PI의 숫자를 대기열에 넣고 검색하는 프로그램입니다.
import java.util.LinkedList;
class Test {
public static void main(String args[]) {
char arr[] = {3,1,4,1,5,9,2,6,5,3,5,8,9};
LinkedList<Integer> fifo = new LinkedList<Integer>();
for (int i = 0; i < arr.length; i++)
fifo.add (new Integer (arr[i]));
System.out.print (fifo.removeFirst() + ".");
while (! fifo.isEmpty())
System.out.print (fifo.removeFirst());
System.out.println();
}
}
또한, 당신이 경우 알 만, 그냥 사용할 수 있습니다 (연결리스트의 추가 기능없이) 대기열로 취급 할 Queue
인터페이스 자체를 :
import java.util.LinkedList;
import java.util.Queue;
class Test {
public static void main(String args[]) {
char arr[] = {3,1,4,1,5,9,2,6,5,3,5,8,9};
Queue<Integer> fifo = new LinkedList<Integer>();
for (int i = 0; i < arr.length; i++)
fifo.add (new Integer (arr[i]));
System.out.print (fifo.remove() + ".");
while (! fifo.isEmpty())
System.out.print (fifo.remove());
System.out.println();
}
}
이렇게 Queue
하면 코드를 너무 많이 변경하지 않고도 기본 구체적인 클래스를 인터페이스 를 제공하는 모든 클래스로 바꿀 수 있다는 장점이 있습니다 .
기본 변경 사항은의 유형 fifo
을 a 로 변경하고 대신 Queue
사용하는 remove()
것입니다 removeFirst()
. 후자는 Queue
인터페이스 에서 사용할 수 없습니다 .
호출 isEmpty()
은 파생 된 Collection
인터페이스에 속하기 때문에 여전히 괜찮습니다 Queue
.
둘 다 인터페이스를 구현하는 ArrayDeque
또는을 시도하십시오 .LinkedList
Queue
http://docs.oracle.com/javase/6/docs/api/java/util/ArrayDeque.html
Queues are First In First Out structures. You request is pretty vague, but I am guessing that you need only the basic functionality which usually comes out with Queue structures. You can take a look at how you can implement it here.
With regards to your missing package, it is most likely because you will need to either download or create the package yourself by following that tutorial.
You don't have to implement your own FIFO Queue, just look at the interface java.util.Queue and its implementations
if you want to have a pipe to write/read data, you can use the http://docs.oracle.com/javase/6/docs/api/java/io/PipedWriter.html
You can use LinkedBlockingQueue I use it in my projects. It's part of standard java and quite easy to use
ReferenceURL : https://stackoverflow.com/questions/9580457/fifo-class-in-java
'Programing' 카테고리의 다른 글
문자열에서 하이픈을 제거하는 가장 빠른 방법 [js] (0) | 2020.12.15 |
---|---|
키보드 단축키를 사용하여 Intellij에서 파일을 닫는 방법은 무엇입니까? (0) | 2020.12.15 |
HTML 스크립트 태그에서 비동기 및 지연 속성을 모두 사용할 수 있습니까? (0) | 2020.12.15 |
PostgreSQL에서 외래 키가있는 행 삭제 (0) | 2020.12.15 |
Get path and query string from URL using javascript (0) | 2020.12.15 |