基于数组的基于队列的泛型是不可能的?(Is it not possible to have an array based Queue with generics ? [duplicate])
这个问题在这里已经有了答案:
- 如何在Java中创建通用数组? 29个答案
这是一个基于数组的队列,用于
int
:/** * Array based * @author X220 * */ public class MyQueue { private int[] _data; private int MAX_SIZE; private int front = -1; private int back = 0; private int elementsCount = 0; public void printQueue() { int j = this.front + 1; int i = 0; while (i < this._data.length && i < elementsCount) { System.out.println("At location " + j % MAX_SIZE + " element :" + this._data[j % MAX_SIZE]); j++; i++; } } public MyQueue(int _size) { MAX_SIZE = _size > 0 ? _size : 10; this._data = new int[MAX_SIZE]; } public boolean IsEmpty() { return this.elementsCount == 0; } public boolean IsFull() { return this.elementsCount == MAX_SIZE; } public void Push(int pushMe) throws QueueIsFullException { if (IsFull()) { throw new QueueIsFullException("Queue is full"); } this.elementsCount++; _data[back++ % MAX_SIZE] = pushMe; } public int Pop() throws QueueIsEmptyException { if (IsEmpty()) { throw new QueueIsEmptyException("Queue is full"); } elementsCount--; return _data[++front % MAX_SIZE]; } public static void main(String args[]) { try { MyQueue q1 = new MyQueue(15); q1.Push(1); q1.Push(2); q1.Push(3); q1.Push(4); q1.Push(5); q1.Pop(); q1.Pop(); q1.Pop(); q1.Pop(); q1.Pop(); q1.Push(6); q1.Pop(); q1.Push(7); q1.Push(8); q1.Push(9); q1.Push(10); q1.Push(11); q1.Push(12); // q1.Push(1); // q1.Push(2); // q1.Push(3); // q1.Push(4); // q1.Push(5); // q1.Push(7); // q1.Push(8); // q1.Push(9); // q1.Push(10); // q1.Push(11); // q1.Push(12); // q1.Push(40); // q1.Push(50); q1.printQueue(); } catch (Exception e) { System.out.println(e); } } @SuppressWarnings("serial") class QueueIsFullException extends Exception { public QueueIsFullException(String message){ super(message); } } @SuppressWarnings("serial") class QueueIsEmptyException extends Exception { public QueueIsEmptyException(String message){ super(message); } } }
我想使用泛型,所以我将
int
改为T
但后来我得到了这个:public class MyQueue <T>{ private T[] _data; private int MAX_SIZE; private int front = -1; private int back = 0; private int elementsCount = 0; public void printQueue() { int j = this.front + 1; int i = 0; while (i < this._data.length && i < elementsCount) { System.out.println("At location " + j % MAX_SIZE + " element :" + this._data[j % MAX_SIZE]); j++; i++; } } public MyQueue(int _size) { MAX_SIZE = _size > 0 ? _size : 10; this._data = new T[MAX_SIZE]; } .... }
那:
- 无法创建T的通用数组
从这篇文章的答案中,我发现我不能在数组中使用泛型。
这是否意味着基于数组的泛型Queue没有解决方法? 我必须切换到其他数据结构吗?
This question already has an answer here:
- How to create a generic array in Java? 29 answers
This is an array based Queue , for
int
:/** * Array based * @author X220 * */ public class MyQueue { private int[] _data; private int MAX_SIZE; private int front = -1; private int back = 0; private int elementsCount = 0; public void printQueue() { int j = this.front + 1; int i = 0; while (i < this._data.length && i < elementsCount) { System.out.println("At location " + j % MAX_SIZE + " element :" + this._data[j % MAX_SIZE]); j++; i++; } } public MyQueue(int _size) { MAX_SIZE = _size > 0 ? _size : 10; this._data = new int[MAX_SIZE]; } public boolean IsEmpty() { return this.elementsCount == 0; } public boolean IsFull() { return this.elementsCount == MAX_SIZE; } public void Push(int pushMe) throws QueueIsFullException { if (IsFull()) { throw new QueueIsFullException("Queue is full"); } this.elementsCount++; _data[back++ % MAX_SIZE] = pushMe; } public int Pop() throws QueueIsEmptyException { if (IsEmpty()) { throw new QueueIsEmptyException("Queue is full"); } elementsCount--; return _data[++front % MAX_SIZE]; } public static void main(String args[]) { try { MyQueue q1 = new MyQueue(15); q1.Push(1); q1.Push(2); q1.Push(3); q1.Push(4); q1.Push(5); q1.Pop(); q1.Pop(); q1.Pop(); q1.Pop(); q1.Pop(); q1.Push(6); q1.Pop(); q1.Push(7); q1.Push(8); q1.Push(9); q1.Push(10); q1.Push(11); q1.Push(12); // q1.Push(1); // q1.Push(2); // q1.Push(3); // q1.Push(4); // q1.Push(5); // q1.Push(7); // q1.Push(8); // q1.Push(9); // q1.Push(10); // q1.Push(11); // q1.Push(12); // q1.Push(40); // q1.Push(50); q1.printQueue(); } catch (Exception e) { System.out.println(e); } } @SuppressWarnings("serial") class QueueIsFullException extends Exception { public QueueIsFullException(String message){ super(message); } } @SuppressWarnings("serial") class QueueIsEmptyException extends Exception { public QueueIsEmptyException(String message){ super(message); } } }
I wanted to use generics so I changed the
int
toT
but then I got for this :public class MyQueue <T>{ private T[] _data; private int MAX_SIZE; private int front = -1; private int back = 0; private int elementsCount = 0; public void printQueue() { int j = this.front + 1; int i = 0; while (i < this._data.length && i < elementsCount) { System.out.println("At location " + j % MAX_SIZE + " element :" + this._data[j % MAX_SIZE]); j++; i++; } } public MyQueue(int _size) { MAX_SIZE = _size > 0 ? _size : 10; this._data = new T[MAX_SIZE]; } .... }
That :
- Cannot create a generic array of T
And from the answers to this post I see that I can't use generics with arrays .
Does this mean that there is no work around for a generics Queue based on array ? Must I switch to some other data structure ?
原文:https://stackoverflow.com/questions/32536594
最满意答案
选项1
正如您现在所做的那样,只需对您的
$.get
进行一次调用,但通过您的function
传递您点击的element
引用,如下所示:<ul> <li><a href="#" onclick="javascript:test2(this);" value="#CS" >CS</a></li> <li><a href="#" onclick="javascript:test2(this);" value="#PS" >PS</a></li> </ul>
JS
function test2(ctrl) { var element=$(ctrl).attr('value'); $.get('external/list.html', function (data) {
$(数据).filter(元件).appendTo( '#内容');//element will have value either #PS or #CS }); }
例如:
$(data).filter(element).html('#content');
选项2
您还可以将
click
事件附加到您a
标签,并为两者提供公共类,从而删除内联function
调用,如下所示:<ul> <li><a href="#" value="#CS" class="menu">CS</a></li> <li><a href="#" value="#PS" class="menu">PS</a></li> </ul>
JS
$(".menu").on('click',function(){ var element=$(this).attr('value'); //this refers to clicked element $.get('external/list.html', function (data) {
$(数据).filter(元件).appendTo( '#内容');//element will have value either #PS or #CS }); });
更新
理想情况下
html
应该可以工作,但我以错误的方式强加它。 你只需要改变一件事如下:$("#content").html($(data).filter(element));
Option 1
Keep a single call to your
$.get
, as you are doing now, but pass reference of your clickedelement
through yourfunction
as below:<ul> <li><a href="#" onclick="javascript:test2(this);" value="#CS" >CS</a></li> <li><a href="#" onclick="javascript:test2(this);" value="#PS" >PS</a></li> </ul>
JS
function test2(ctrl) { var element=$(ctrl).attr('value'); $.get('external/list.html', function (data) {
$(data).filter(element).appendTo('#content');//element will have value either #PS or #CS }); }
Note -
.appendTo
will keep on appending the data to yourDOM
, instead use.html
which replaces contents everytimeEx:
$(data).filter(element).html('#content');
Option 2
You can also attach
click
event to youra
tags with a common class given for both thus removing inlinefunction
call as below:<ul> <li><a href="#" value="#CS" class="menu">CS</a></li> <li><a href="#" value="#PS" class="menu">PS</a></li> </ul>
JS
$(".menu").on('click',function(){ var element=$(this).attr('value'); //this refers to clicked element $.get('external/list.html', function (data) {
$(data).filter(element).appendTo('#content');//element will have value either #PS or #CS }); });
Update
Ideally
html
should work but I imposed it in wrong way. You just need to change one thing as below:$("#content").html($(data).filter(element));
相关问答
更多-
我认为这是使用jQuery将html内容/文件包含到另一个html文件中的最佳方式。 您可以简单地包含jQuery.js并使用$("#DivContent").load("yourFile.html"); HTML文件$("#DivContent").load("yourFile.html"); 例如JavaScript方法 两个Html你有: Html文件1:
10
100
居中html div(Center a html div)[2022-11-25]
您可以使用下面的代码 section > div { position: absolute; left: 0; right: 0; top: 50%; transform: translateY(-50%); } 查看示例 You can use the following code section > div { position: absolute; left: 0; right: 0; top: 50%; transfo ...如果您检查控制台,则会发现此错误 未捕获的ReferenceError:$未定义 您需要导入Jquery Library以处理JSFIDDLE上的项目 要么: $(document).ready(function() { $('#myDiv').load('https://static.webshopapp.com/shops/054833/files/057957154/needtoknow.html'); });