小编Dus*_*tin的帖子

compareTo方法需要一些工作

我需要按照城市的字母顺序将城市和州分类为一个arraylist,但如果两个城市的名字相同,那么州将成为决胜局.

public class City implements Comparable  
{
   String name;
   String state;

   /**
   ** A constructor for the city and state.
   ** @param name the name of the city.
   ** @param state the name of the state.
   */   
   public City(String name, String state)
   {
      this.name = name;
      this.state = state;
   }
   //Gets the name and returns it.
   public String getName()
   {
      return name;
   }
   //Gets the state and returns it.
   public String getState()
   {
      return state;
   } 
   public int compareTo(Object otherCity) …
Run Code Online (Sandbox Code Playgroud)

java

3
推荐指数
1
解决办法
367
查看次数

如何使用迭代器测试字符串是否以元音开头

我第一次使用迭代器编写了一个程序.我有一个字符串数组列表,我使用hasNext和next方法打印它们.我现在正试图使用​​前面的方法向后退,并且在这样做时我想检查每个元素,如果它以元音开头,我要从列表中删除它.这是我遇到问题的一段代码.

while (iterator.hasPrevious())
      {
         String s = iterator.previous();
         if (s.startsWith("a"))
         {
            iterator.remove();
         }   
         System.out.print(" "+ s);
      }
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激!

java iterator

0
推荐指数
1
解决办法
3705
查看次数

标签 统计

java ×2

iterator ×1