【问题标题】:ArrayList and stringsArrayList 和字符串
【发布时间】:2014-10-12 21:05:57
【问题描述】:

我有这个问题写一个静态方法,它接受一个字符串的 ArrayList 和一个整数,并破坏性地更改 ArrayList 以删除所有长度小于整数参数的字符串。到目前为止我有这个代码有人可以解释我哪里出错了。它可以编译,但不会从数组列表中删除任何字符串。

import java.util.*;

public class q4
// Shows adding a string after all occurrences of a string
// constructively in an ArrayList
{

   public static void main(String[] args) throws Exception
   {

      Scanner input = new Scanner(System.in);

      System.out.println("Enter some words (all on one line, separated by spaces):");
      String line = input.nextLine();
      String[] words = line.split(" +");
      ArrayList<String> a = new ArrayList<String>();

      for(int i=0; i<words.length; i++)
      {
         a.add(words[i]);
      }

      System.out.println("The words are stored in an ArrayList");
      System.out.println("The ArrayList is "+a);

      System.out.print("\nEnter a number");

      int len = input.nextInt();

      for(int j=0;j<words.length;j++)
      {
         String b =a.get(j);

         if(b.length()<len)
         {

            a.remove(j);

         }

      } 

      System.out.println("The ArrayList is "+a);

   }

}

【问题讨论】:

  • 到底是什么问题?你有例外吗?输出错了吗?
  • 第一个问题是在浏览器中输入格式不正确的代码并期待帮助。请正确缩进,这样帮助您的人就不必为满足您的需要而做更多的工作。
  • 我已格式化您的代码 OP,请接受修改
  • 您好,我是新来的帮助工具栏,据说每行代码前有 4 个空格键。所以这就是我所做的。
  • 谢谢dreadheadeddeveloper

标签: java string arraylist


【解决方案1】:

当您删除 ArrayList 的一项时,请务必减少“j”。另外,虽然不常见,但将 for-condition 设置为j &lt; a.size()。否则,创建一个单独的变量来存储循环之前的大小,然后也将其递减。 以下代码应该可以工作。

public static void main(String[] args)
{

    Scanner input = new Scanner(System.in);

    System.out.println("Enter some words (all on one line, separated by spaces):");
    String line = input.nextLine();
    String[] words = line.split(" +");
    ArrayList<String> a = new ArrayList<String>();

    for(int i=0; i<words.length; i++)
    {
        a.add(words[i]);
    }

    System.out.println("The words are stored in an ArrayList");
    System.out.println("The ArrayList is "+a);

    System.out.print("\nEnter a number");

    int len = input.nextInt();

    for(int j=0;j<a.size(); j++)
    {
        String b =a.get(j);

        if(b.length()<len)
        {

            a.remove(j);
            j--;
        }

    } 

    System.out.println("The ArrayList is "+a);
}

【讨论】:

  • 仍然是同样的错误,当数组列表在删除项目后在最后打印时仍然相同。
  • 您是从我的答案中复制粘贴代码还是进行了我所做的所有更改?因为它对我有用。
【解决方案2】:

您从左到右遍历列表并删除项目。这在删除多个项目时会导致问题,因为索引不再匹配。

这可以通过从头到尾而不是从头到尾遍历列表来很容易地解决。

因为现在,如果您移除一个项目,这不会影响以后要移除的项目:

for (int j = words.length - 1; j >= 0; j--) 

【讨论】:

  • 我认为你的方式也会搞砸。直接使用迭代器。或者保留输入的 ArrayList,将长度小于整数的每个字符串移动到一个新的 ArrayList 中。
  • 你好,我试过了,还是一样的错误。
  • @user3439272 为什么不使用迭代器进行迭代,而是调用.remove()
  • @user3439272 你甚至从未说过你遇到了什么错误。我只是假设它是一个 IndexOutOfBounds,这个解决方案修复了它。使用示例数据及其提供的 Stacktrace 更新您的问题。
  • 数组列表基本不变。
【解决方案3】:
import java.util.*;

public class q4
// Shows adding a string after all occurrences of a string
// constructively in an ArrayList
{

   public static ArrayList<String> a;
   public static ArrayList<String> presenter;

   public static void main(String[] args) throws Exception
   {

      Scanner input = new Scanner(System.in);

      System.out.println("Enter some words (all on one line, separated by spaces):");
      String line = input.nextLine();
      String[] words = line.split(" +");
      a = new ArrayList<String>();
      presenter = new ArrayList<String>();

      for(int i=0; i<words.length; i++)
      {

         a.add(words[i]);

      }

      System.out.println("The words are stored in an ArrayList");
      System.out.println("The ArrayList is "+a);

      System.out.print("\nEnter a number");

      int len = input.nextInt();

      for(int j=0;j<words.length;j++)
      {

         String b =a.get(j);

         if((b.length()<len))
         {

            //do nothing

         }

         else
         {

            presenter.add(a.get(j));

         }

      } 

      System.out.print("The ArrayList is " + presenter);

   }
}

这是一个替代方案,因为你说其他代码不起作用,我只是将“好”数据转移到另一个数组列表并打印出来。

【讨论】:

    猜你喜欢
    • 2021-03-04
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    相关资源
    最近更新 更多