gpt4 book ai didi

java - 带有通用数组的循环?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:36:06 25 4
gpt4 key购买 nike

在这里回顾一下我的基本 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++;
}
}

应该使用链表从数组中添加通用项。不幸的是,它没有,这是我遇到的第一个问题。我收到错误:

cannot find symbol : method length().

有人可以就如何解决这个问题给我一些建议吗?

非常感谢!

最佳答案

在数组上你没有 length() 方法但有一个 length 成员:array.length

此外,您需要在 countArray 达到 array.length 之前停止迭代并在使用前初始化大小:

final int arrayLength = array.length;
size = arrayLength;
nodes = null;
for(int i = 0; i < arrayLength; ++i) {
nodes = new NodeList(array[i], nodes);
}

nodes = null;
size = array.length;
for(T element : array) {
nodes = new NodeList(element, nodes);
}

关于java - 带有通用数组的循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5183220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com