从列表创建字母列表:Java [英] Create Alphabet List from list : Java

查看:120
本文介绍了从列表创建字母列表:Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,其中包含来自给定路径的文件名,

I have a list which contains the Filename from a given path,

fileNamesList

,然后从该列表中创建一个Alphabet list,例如如果文件名以A开头,然后将A添加到列表中,直到Z,并以相同的方式创建也来自0 to 9

and from this list i would like to create a Alphabet list like if there are files name start with A then add A to list and like so upto Z and in the same way i want to create Numbers List also from 0 to 9

所有这些字母和数字检查均基于Starts with from the file name.

All these Alphabet and Number checking is based on Starts with from the file name.

如何在Java中执行此操作.

how to do this in java.

最后,AlphabetList will be like A B E F....ZNumbers list will be 1 2 3 9

谢谢

推荐答案

初始化AlphabetList后:

for (int i = 'A'; i <= 'Z'; i++)
    for (fileName: fileNameList) {
        if ((int)fileName.charAt(0) == i) {
            AlphabetList.add((char)i);
        }
    }
}

您可以为NumbersList做类似的事情.

You can do a similar thing for NumbersList.

或者:

occurrences = new int[256];
for (fileName: fileNameList) {
    occurrences[(int)fileName.charAt(0)] += 1;
}
for (int i = 'A', i <= 'Z'; i++) {
    if (occurrences[i] > 0) {
        AlphabetList.add((char)i)
    }
}
for (int i = '0', i <= '9'; i++) {
    if (occurrences[i] > 0) {
        NumberList.add(i - '0')
    }
}

这篇关于从列表创建字母列表:Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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