Java通用循环缓冲区排序
我有一个任务来实现一个通用的循环缓冲区。 一切都很好,我只需要做一个Java通用循环缓冲区排序,java,sorting,generics,circular-buffer,Java,Sorting,Generics,Circular Buffer,我有一个任务来实现一个通用的循环缓冲区。 一切都很好,我只需要做一个排序方法,但是我想不出一个通用的解决方案。 你能给我一个提示吗? 谢谢大家! public class CircularBuffer<T> { public T[] elements = null; private int capacity = 0; private int dataStart = 0; private int dataEnd = 0; @SuppressWarnings("uncheck
排序
方法,但是我想不出一个通用的解决方案。
你能给我一个提示吗?
谢谢大家!
public class CircularBuffer<T> {
public T[] elements = null;
private int capacity = 0;
private int dataStart = 0;
private int dataEnd = 0;
@SuppressWarnings("unchecked")
public CircularBuffer(int capacity) {
this.capacity = capacity;
this.elements = (T[]) new Object[capacity];
}
public boolean isEmpty() {
return dataStart == dataEnd;
}
public boolean isFull() {
if (dataStart == 0) {
return dataEnd == capacity - 1 ;
}
return dataStart - dataEnd == 1;
}
public int size() {
return dataEnd - dataStart;
}
public void put(T t) {
if (isFull()) {
throw new RuntimeException("Buffer is full");
}
if (dataEnd < capacity) {
elements[dataEnd] = t;
dataEnd++;
}
}
public T get() {
if (isEmpty()) {
throw new RuntimeException("Buffer is empty");
}
return elements[dataStart++];
}
public Object[] toObjectArray() {
Object[] newArray = new Object[size()];
for (int i = dataStart; i < dataEnd; i++) {
newArray[i] = elements[i];
}
return newArray;
}
@SuppressWarnings("unchecked")
public <Q> Q[] toArray(Q[] a) {
if (a.length < size())
return (Q[]) Arrays.copyOf(elements, size(), a.getClass());
System.arraycopy(elements, 0, a, 0, size());
return a;
}
public List<T> asList(List<T> a) {
List<T> list = new ArrayList<>(size());
for (int i = dataStart; i < dataEnd; i++) {
list.add(elements[i]);
}
return list;
}
public void addAll(List<? extends T> toAdd) {
if (toAdd.size() > capacity - size()) {
throw new RuntimeException("Not enough space to add all elements");
}
else {
for (int i = 0; i < toAdd.size(); i++) {
elements[dataEnd] = toAdd.get(i);
dataEnd++;
}
}
}
public void sort(Comparator<? super T> comparator) {
// TODO
}
}
公共类循环缓冲{
公共T[]元素=null;
专用int容量=0;
私有int dataStart=0;
私有int dataEnd=0;
@抑制警告(“未选中”)
公共循环缓冲器(国际容量){
这个。容量=容量;
this.elements=(T[])新对象[容量];
}
公共布尔值为空(){
返回dataStart==dataEnd;
}
公共布尔值isFull(){
如果(dataStart==0){
返回dataEnd==容量-1;
}
返回dataStart-dataEnd==1;
}
公共整数大小(){
返回dataEnd-dataStart;
}
公开作废认沽权(T){
如果(isFull()){
抛出新的RuntimeException(“缓冲区已满”);
}
if(数据端<容量){
元素[dataEnd]=t;
dataEnd++;
}
}
公共部门得不到{
if(isEmpty()){
抛出新的RuntimeException(“缓冲区为空”);
}
返回元素[dataStart++];
}
公共对象[]ToObject数组(){
Object[]newArray=新对象[size()];
for(int i=dataStart;i
A) 这不是循环-当索引通过容量时,您应该重置dataStart
和dataEnd
。一种简单的方法是替换以下内容:
dataEnd++;
与:
B) 一旦你这样做了,我猜“排序”只是指介于dataStart
到dataEnd
之间的部分。由于循环的性质,这并不简单。我认为你需要在循环缓冲区上实现一种排序。我会这样做:
public void sort(Comparator<? super T> comparator) {
if (dataStart > dataEnd) {
System.arraycopy(elements, dataStart, elements, dataEnd, capacity-dataStart);
dataEnd = dataEnd + capacity-dataStart;
dataStart = 0;
}
Arrays.sort(elements, dataStart, dataEnd, comparator);
}
public void sort(Comparator最简单的选择是将内容作为列表
取出,对其进行排序,并替换当前已排序列表中的旧内容
public void sort(Comparator<? super T> comparator) {
// Get them all out - not sure why you have a parameter to `asList`
List<T> all = asList(Collections.emptyList());
// Sort them.
Collections.<T>sort(all);
// Clear completely.
dataStart = dataEnd = 0;
addAll(all);
}
你的具体问题是什么?乍一看,你已经正确地设置了泛型。关于A:是的,我知道,这是我最初实现它的方式,但是任务的描述说如果缓冲区已满,put方法将引发运行时异常。当头部和尾部指向同一索引时,缓冲区已满,但缓冲区不是空的"通过这一点,我理解了它不是真正的循环。所以,让我们说它不是真正的循环…排序如何是泛型的?关于循环,您在第一次就得到了正确的答案。其余的解释起来很复杂。关于asList
:我需要知道它将返回的列表的类型。它从参数中获取类型er并创建该类型的列表。@Kokufuu-它只是忽略参数。除非您计划更改它-这很好。请更正我,但ArrayList不会自动将类型t
放入
?@kokufu-是,但它是从类泛型参数t
获取的,而不是参数传递给asList
。
public void sort(Comparator<? super T> comparator) {
// Get them all out - not sure why you have a parameter to `asList`
List<T> all = asList(Collections.emptyList());
// Sort them.
Collections.<T>sort(all);
// Clear completely.
dataStart = dataEnd = 0;
addAll(all);
}
public class CircularBuffer<T extends Comparable<T>> {