带有通用数组的 for 循环?

发布于 2024-10-20 04:09:48 字数 891 浏览 5 评论 0原文

在这里回顾一下我的基本 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

别念他 2024-10-27 04:09:48

在数组上,您没有 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);
}

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:

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

or

nodes = null;
size = array.length;
for(T element : array) {
      nodes = new NodeList(element, nodes);
}
风情万种。 2024-10-27 04:09:48

集合类上的方法是 .size(),数组上的方法是 .length 属性。

但是您可以使用“增强型”for 循环(又名 foreach)来遍历其中任何一个:

for( T element : array ) {
    nodes = new NodeList( value, nodes );
    size++;
}

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):

for( T element : array ) {
    nodes = new NodeList( value, nodes );
    size++;
}
一曲琵琶半遮面シ 2024-10-27 04:09:48

除了其他人发布的内容之外,您可能还想使用通用参数 T:

public class NodeList<T> {
  private T head;
  private NodeList<T> tail;
  public NodeList( T item, NodeList list ) {
    head = item;
    tail = list;
  }
}

In addition to what others have posted, you might also want to use your generic parameter T:

public class NodeList<T> {
  private T head;
  private NodeList<T> tail;
  public NodeList( T item, NodeList list ) {
    head = item;
    tail = list;
  }
}
撕心裂肺的伤痛 2024-10-27 04:09:48

length 是一个 字段,不是数组上的方法。删除括号。

for(int countArray = 0; countArray <= array.length ; countArray++) {
  nodes= new NodeList( value, nodes );
  size++;
}

这是编写整个构造函数的更好方法:

public MyList(T[] array ){
    nodes = null;
    for(T t : array) {
        nodes = new NodeList(t, nodes);
    }
    size = array.length;
}

length is a field, not a method, on arrays. Remove the parentheses.

for(int countArray = 0; countArray <= array.length ; countArray++) {
  nodes= new NodeList( value, nodes );
  size++;
}

Here's a better way to write the whole constructor:

public MyList(T[] array ){
    nodes = null;
    for(T t : array) {
        nodes = new NodeList(t, nodes);
    }
    size = array.length;
}
满天都是小星星 2024-10-27 04:09:48

如果您想确保只有可比较的项目是可能的:

public class NodeList<T extends Comparable<T> > {
  private T head;
  private NodeList<T> tail;
  public NodeList( T item, NodeList<T> list ) {
    head = item;
   tail = list;
  }
}

此外

public class MyList<T extends Comparable<T>> {
...
}

,如果您的构造函数使用 var args,您将获得更方便的创建列表的方法:

public MyList(T... array ) {
  for( T item : array ) {
    nodes = new NodeList<T>(item, nodes); 
  }
  size = array.length;
}

这样您可以按如下方式调用构造函数:

new MyList<Long>(); //empty list
new MyList<Long>( 1L ); //one entry
new MyList<Long>( 1L, 2L, 3L ); //3 entries
Long[] array = new Long[] { 1L, 2L, 3L, 4L };
new MyList<Long>( array ); //use existing array

If you want to make sure that only comparable items are possible:

public class NodeList<T extends Comparable<T> > {
  private T head;
  private NodeList<T> tail;
  public NodeList( T item, NodeList<T> list ) {
    head = item;
   tail = list;
  }
}

And

public class MyList<T extends Comparable<T>> {
...
}

Additionally, if your constructor uses var args, you get a more convenient way of creating a list:

public MyList(T... array ) {
  for( T item : array ) {
    nodes = new NodeList<T>(item, nodes); 
  }
  size = array.length;
}

That way you can call the constructor as follows:

new MyList<Long>(); //empty list
new MyList<Long>( 1L ); //one entry
new MyList<Long>( 1L, 2L, 3L ); //3 entries
Long[] array = new Long[] { 1L, 2L, 3L, 4L };
new MyList<Long>( array ); //use existing array
烧了回忆取暖 2024-10-27 04:09:48

它是 array.length 而不是 array.length()。

for(int countArray = 0; countArray <= array.length ; countArray++) {

将解决您的编译错误。

It is array.length not array.length().

for(int countArray = 0; countArray <= array.length ; countArray++) {

will resolve your compilation error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文