简体   繁体   English

使用ListIterator从要连接的列表的末尾开始

[英]Using ListIterator to start at the end of a list to concatenate

So what I am trying to do is take the list I have and start at the end. 所以我想做的就是列出我已有的列表,然后从头开始。 From there I need to go backwards and return the list as a String. 从那里,我需要向后退,并以字符串形式返回列表。 So my array has "Words are flowing out like endless rain into a paper cup". 所以我的数组有“单词像无尽的雨水一样涌入纸杯中”。 I'm trying to make them cuppaperainto... and so on. 我正在尝试使它们成为cuppaperainto ...等等。 This is my code but it is giving me a symbol not found error on my list.size and list.listIterator. 这是我的代码,但它在list.size和list.listIterator上给了我一个找不到符号的错误。 Just wondering what I'm doing wrong and how to fix this problem. 只是想知道我在做什么错以及如何解决此问题。

public class LabListIterators {

public static void main(String[] args) {

    List<String> list = new LinkedList<String>();

    // Add items to the List
      String [] array = {"Words", "are", "flowing", "out", "like", "endless", "rain", "into", "a", "paper", "cup"};
      for (int i = 0; i < array.length; ++i)
            list.add (array[i]);
    System.out.println(list + ", size = " + list.size());
    capitalize(list);
    System.out.println(list);
    System.out.println(concatenateBackwards(list));      
}
public static String concatenateBackwards(List<String> words)
 {
  ListIterator iter = list.listIterator(list.size());
  while (iter.hasPrevious())
  {
     String str = iter.previous() + str; 
  }
  return str;  
 }
}

You just got the names and the scope mixed up a little from this code block 您刚刚从该代码块中获得了名称和作用域的混淆

public static String concatenateBackwards(List<String> words)
{
   ListIterator iter = list.listIterator(list.size());
   while (iter.hasPrevious())
  {
     String str = iter.previous() + str; 
  }
  return str;  
}
  1. The method takes in List<String> words but you are trying to call listIterator() on a object list which I think you wanted to use when you created within the main() method. 该方法使用List<String> words但是您尝试在对象list上调用listIterator() ,我认为您要在main()方法中创建该对象时要使用该list

  2. Inside the while loop: 在while循环内:

     String str = iter.previous() + str; 

    You have to declare str first before you can use it. 您必须先声明str才能使用它。 So move the str declaration before the while loop. 因此将str声明移到while循环之前。 Also, the return str; 另外, return str; does not know about the scope of tr because it is declared within the while loop body. 不知道tr的范围,因为它是在while循环体内声明的。

Solution: 解:

public static String concatenateBackwards(List<String> words)
{
   String str = "";
   ListIterator iter = words.listIterator(list.size());
   while (iter.hasPrevious())
  {
     str += iter.previous();
  }
  return str;  
}

You are declaring the String inside the loop 您正在循环中声明String

  while (iter.hasPrevious())
  {
     String str = iter.previous() + str; 
  }
  return str; 

Try 尝试

  String str ="";
  while (iter.hasPrevious())
  {
     str = iter.previous() + str; 
  }
  return str; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM