【发布时间】: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