【问题标题】:Taking an element of An ArrayList and passing it as an argument [closed]获取一个 ArrayList 的元素并将其作为参数传递[关闭]
【发布时间】:2015-04-18 08:51:48
【问题描述】:

我对这个 ArrayList 有点不满。我正在尝试将字符串数组中的一个或多个元素存储到数组列表中,并将其分配给某个字符串变量。我的目标是将关键字存储到一个数组列表中,我可以用它来搜索文本文件。我似乎无法将找到的关键字存储到数组列表中有人可以帮我解决这个问题吗?这是我的代码中的一些 sn-ps。

    public static void main(String args[]) throws ParseException, IOException        
    {
List<String> matches = new ArrayList<String>();
String[] keywords = {"day", "book", "office", "hour",
        "date of a test", "number of assignments", "sure",
        "current assignment", "due day"};
     System.out.println("What would you like to know?");

        System.out.print("> ");
        input = scanner.nextLine().toLowerCase();

         int count = 0;
        for (int i = 0; i < keywords.length; i++) {
            if (input.contains(keywords[i])) {


            matches.add(keywords[i]);

                parseFile(keywords[i]);
    }
}

  }

这是我的 parseFile 方法

     public static void parseFile(String s) throws FileNotFoundException {
    File file = new File("data.txt");

    Scanner scanner = new Scanner(file);
    while (scanner.hasNextLine()) 
    {
        final String lineFromFile = scanner.nextLine();
        if (lineFromFile.contains(s)) {
            // a match!
            System.out.println(lineFromFile);
            // break;
        }

    }
}

【问题讨论】:

  • 有什么问题?请更具体。
  • 在您的评论中,您问如何将关键字存储在字符串中 - 您希望字符串具有什么格式?
  • 取决于您希望如何格式化 String。您可以使用 StringBuilder 并在此处附加每个关键字。或者您可以在末尾使用 matches.toString()
  • 由于问题还不清楚,这里有一些改进代码的建议:你正在调用 parseFile(keywords[i]);每场比赛。这样,您将打开文件并多次阅读,这是不必要的。一旦你有了完整的匹配列表,你可以将它作为参数提供给 parseFile。另外,当你读完文件后关闭它!最后, contains() 也将匹配子字符串(“booking”包含“book”) - 这是你需要的吗?

标签: java arrays list arraylist


【解决方案1】:

我要做的第一件事就是检查这些东西是否真的进入了数组,所以我有这个:

if(matches.size() == 0)
{
    System.out.println("There's nothing in here");
}

这样至少你知道那里什么都没有,那么剩下的程序就没有意义了。您应该尽可能早地测试您的退出条件,这样可以节省一些时间和精力,并允许更快的调试。但我离题了。

因此,要将内容添加到数组列表中,您需要执行以下操作:

for (int i = 0; i < keywords.length; i++) 
{
   String value = keywords[i];
   System.out.println("Current Value is: " + value);
   matches.add(value);
}

您不能只添加一个数组元素,因为 ListArray 中的 add 方法需要一个字符串。所以你需要将数组的当前内容设置为一个字符串,然后将它传递给你的matches.add函数,所以当我在我的盒子上运行那个程序时,(只是主要的一点)我得到了以下输出.

Current Value is: day
Current Value is: book
Current Value is: office
Current Value is: hour
Current Value is: date of a test
Current Value is: number of assignments
Current Value is: sure
Current Value is: current assignment
Current Value is: due day
Size of matches is: 9
Matches[i]: day
Matches[i]: book
Matches[i]: office
Matches[i]: hour
Matches[i]: date of a test
Matches[i]: number of assignments
Matches[i]: sure
Matches[i]: current assignment
Matches[i]: due day

另外,为了记录,您在遍历匹配项时也需要执行相同的操作,因此假设您要打印出 ArrayList,那么您需要执行以下操作:

for(int i = 0; i < matches.size(); i++)
{
    String value = matches.get(i);
    System.out.println("Matches[i]: " + value);
}

你需要在数组列表中获取字符串,你不能只给它一个索引,因为这不起作用,你需要再次将它分配给一个字符串,然后将其传回。

如果有疑问,请深入了解 Java ArrayList API,并查看函数采用哪些参数。

希望对您有所帮助。

【讨论】:

  • @Welshboy 好吧假设我输入了一些包含两个匹配关键字的文本,我如何将两个匹配关键字存储在列表中并检索它们?
  • 为什么要存储两个相同的关键字呢?如果您真的想保留两个关键字,那么我会考虑使用地图,但是自从我做了很多Java以来​​已经有几年了,但是我会看一下地图,例如map 其中 int 是键,字符串是值,看一下。希望对你有帮助
  • 不,如果是两个关键字,我想存储关键字。例如,如果“go”和“help”是两个关键字,我想存储它们并检索它们。明白了吗?
猜你喜欢
  • 1970-01-01
  • 2012-11-10
  • 1970-01-01
  • 2016-11-18
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 1970-01-01
相关资源
最近更新 更多