小编A C*_*A C的帖子

Java - 搜索目录中的文件

这应该是简单的,但我无法理解 - "编写一个程序来搜索给定目录中的特定文件名." 我找到了一些硬编码文件名和目录的例子,但是我需要用户输入的目录和文件名.

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)

java directory search file

10
推荐指数
2
解决办法
10万
查看次数

从ArrayList(Java)添加到堆栈

我有一个预定义了硬编码值的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)

谢谢!

java stack arraylist

8
推荐指数
1
解决办法
1万
查看次数

Treeset按字母顺序排序

如何让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)

java sorting treeset

3
推荐指数
1
解决办法
2万
查看次数

字符串中的Java子字符串

我知道这个问题已被无数次问过......但我似乎无法让它发挥作用.问题是,

  1. 当输入"计算机"和"计算机"时,即使将所有内容转换为小写也无效.
  2. 如果字符串是一个句子(我添加一个空格),它实质上跳过子字符串代码并说"不在字符串中"..

感谢帮助.谢谢!

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)

java string substring

0
推荐指数
1
解决办法
487
查看次数

标签 统计

java ×4

arraylist ×1

directory ×1

file ×1

search ×1

sorting ×1

stack ×1

string ×1

substring ×1

treeset ×1