你如何在java中搜索数组中的东西 [英] how do you search for something in an array in java

查看:127
本文介绍了你如何在java中搜索数组中的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个执行此操作的公共非静态方法:

I wan to create a public, non-static method that does this:

getContent:此方法应将String文件名作为输入并返回String。该方法应在数组驱动器中搜索名称为filename的文件,并返回存储在该TxtFile中的数据。如果阵列驱动器中不存在此类文件,则该方法应返回null。

getContent : This method should take as input a String filename and return a String. The method should search for a file with name filename in the array drive and return the data that is stored in that TxtFile. If no such file exists in the array drive, the method should return null.

我不知道如何搜索数组中的内容。有人可以告诉我该怎么做吗?

I don't know how to search for something in an array. can someone show me how to do this?

推荐答案

来吧,自己尝试一下:

第1步:从目录中读取所有文件名。

Step1: Read all the file Names from a directory.

第2步:商店列表中的文件列表。

Step2: Store the list of Files to a List.

Step3:迭代列表,使用File.getName()方法编写条件比较以及名称和输入。

Step3: Iterate the list, Write a conditions with a use the File.getName() method to compare and the name and your input.

if(file.getName().equals(inputFileName)){
   return "boooo! I have found You!"  
}

这篇关于你如何在java中搜索数组中的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆