带有通用数组的 for 循环?
在这里回顾一下我的基本 ADT 内容,并尝试通过学习 Java 来一石二鸟,同时我正在尝试使用通用链表(我自己创建的)编写一个用于合并排序的简单算法。事实证明,这比我最初想象的要困难得多!有人可以帮我吗?我将开始研究基础知识,并在进一步了解后更新这篇文章。
我的通用链表代码如下:
public class NodeList<T> {
private Comparable head;
private NodeList tail;
public NodeList( Comparable item, NodeList list ) {
head = item;
tail = list;
}
}
正在尝试在我制作的另一个类中访问此类,如下所示:
public class MyList<T> {
private NodeList<T> nodes;
private int size;
public MyList( ) {
nodes = null;
}
public MyList(T[] array ){
for(int countArray = 0; countArray <= array.length() ; countArray++) {
nodes= new NodeList( value, nodes );
size++;
}
}
我 应该使用链接列表从数组添加通用项。不幸的是,事实并非如此,这是我遇到的第一个问题。我收到错误:
找不到符号:方法 length()。
有人可以给我一些关于如何解决这个问题的建议吗?
非常感谢!
Going back over my basic ADT stuff here, and trying to kill two birds with one stone by learning Java while I amTrying to write a simple algorithm for a merge sort with a generic linked list ( which I am creating myself). It's proving to be far more difficult than I had first imagined ! Can anyone help me out please ? I will start out working on the basics and will update this post as I get further in.
My code for the generic linked list is as follows :
public class NodeList<T> {
private Comparable head;
private NodeList tail;
public NodeList( Comparable item, NodeList list ) {
head = item;
tail = list;
}
}
I am trying to access this class in another class I have made, which is as follows :
public class MyList<T> {
private NodeList<T> nodes;
private int size;
public MyList( ) {
nodes = null;
}
public MyList(T[] array ){
for(int countArray = 0; countArray <= array.length() ; countArray++) {
nodes= new NodeList( value, nodes );
size++;
}
}
which should add generic items from an array, using a linked list. Unfortunately, it doesn't and this is the first problem I have encountered. I am getting the error :
cannot find symbol : method length().
Can someone give me some advice on how I could fix this?
Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在数组上,您没有 length() 方法,但有一个 length 成员: array.length
另外,您需要在 countArray 达到 array.length 之前停止迭代,并在使用它之前初始化大小:
或
on the array you don't have a length() method but a length member : array.length
Additionally, you'll want to stop iterating before countArray reaches array.length and initialise size before using it:
or
集合类上的方法是
.size()
,数组上的方法是.length
属性。但是您可以使用“增强型”for 循环(又名 foreach)来遍历其中任何一个:
The method on a collections class is
.size()
, or on an array it is the.length
property.But you can loop through either of these with an "enhanced" for loop (aka foreach):
除了其他人发布的内容之外,您可能还想使用通用参数 T:
In addition to what others have posted, you might also want to use your generic parameter T:
length
是一个 字段,不是数组上的方法。删除括号。这是编写整个构造函数的更好方法:
length
is a field, not a method, on arrays. Remove the parentheses.Here's a better way to write the whole constructor:
如果您想确保只有可比较的项目是可能的:
此外
,如果您的构造函数使用 var args,您将获得更方便的创建列表的方法:
这样您可以按如下方式调用构造函数:
If you want to make sure that only comparable items are possible:
And
Additionally, if your constructor uses var args, you get a more convenient way of creating a list:
That way you can call the constructor as follows:
它是 array.length 而不是 array.length()。
将解决您的编译错误。
It is array.length not array.length().
will resolve your compilation error.