这应该是简单的,但我无法理解 - "编写一个程序来搜索给定目录中的特定文件名." 我找到了一些硬编码文件名和目录的例子,但是我需要用户输入的目录和文件名.
public static void main(String[] args) {
String fileName = args[0]; // For the filename declaration
String directory;
boolean found;
File dir = new File(directory);
File[] matchingFiles = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String fileName) {
return true;
}
});
}
Run Code Online (Sandbox Code Playgroud) 我有一个预定义了硬编码值的ArrayList.如何将这些添加到堆栈?这个想法是为了演示堆栈类的pop,push,peek函数.
ArrayList<String> al = new ArrayList<String>();
al.add("A");
al.add("B");
al.add("C");
Stack<String> st = new Stack<String>();
st.push(al); **// This doesn't seem to work.. Will I have to loop it in some way?**
System.out.println(st);
Run Code Online (Sandbox Code Playgroud)
谢谢!
如何让treeset按字母顺序排序?并删除重复..它一直让我疯了一天.也许我需要睡个好觉..
public static void main(String[] args) {
String fileName = args[0];
String words;
Scanner s = null;
Iterator itr;
try {
s = new Scanner(new BufferedReader(new FileReader(fileName)));
while (s.hasNext()) {
words = s.next();
TreeSet<String> ts = new TreeSet<String>();
ts.add(words);
System.out.println(ts);
}
} catch (FileNotFoundException fnfe) {
System.exit(0);
} finally {
if (s != null) {
s.close();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我知道这个问题已被无数次问过......但我似乎无法让它发挥作用.问题是,
感谢帮助.谢谢!
Scanner in=new Scanner(System.in);
System.out.println("\fEnter the main string:");
String GivenString=in.next();
System.out.println("Enter the substring :");
String SubString=in.next();
GivenString.toLowerCase();
SubString.toLowerCase();
if(GivenString.indexOf(SubString)!=-1)
{
System.out.println("Substring is in the given string.");
}
else
{
System.out.println("Substring is not in the given string.");
}
Run Code Online (Sandbox Code Playgroud)