从最终 class 中获取所有静态字符串值作为字符串数组或列表
Get all static String values from a final class as a String Array or List
我有以下 class:
public final class FileNames
{
private FileNames()
{}
public static final String A = "a.csv";
public static final String B = "b.csv";
public static final String C = "123.csv";
...
}
现在我想从上面获取所有 String 值,在不同的某处 class.Output 可以作为 String 数组或 ArrayList 或任何其他 Collection。我正在使用 jdk 1.7。不知道如何继续这个,因为可以增加字符串列表,即可以添加 "D"、"E" 等..
反思是你的朋友:)
public class Sample {
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
Class<FileNames> clazz = FileNames.class;
Field[] arr = clazz.getFields(); // Get all public fields of your class
for (Field f : arr) {
if (f.getType().equals(String.class)) { // check if field is a String
String s = (String)f.get(null); // get value of each field
// add s to a List
System.out.println(s);
}
}
}
}
final class FileNames {
private FileNames() {
}
public static final String A = "a.csv";
public static final String B = "b.csv";
public static final String C = "123.csv";
}
O/P :
a.csv
b.csv
123.csv
你添加这个功能:
public static ArrayList<String> getFilenames() {
ArrayList<String> filenames = new ArrayList<String>;
filenames.add(A);
filenames.add(B);
filenames.add(C);
return filenames;
}
然后你可以在某处使用这个函数:
ArrayList<String> filenames = new ArrayList<String>;
filenames = Filenames.getFilenames();
String filenameA = filenames.get(0); // to retrieve the first element (A)
String filenameB = filenames.get(1); // to retrieve the second element (B)
// and so on...
如果你想添加更多并以自动方式转换它,你可以检查这个:Java method with unlimited arguments
希望能帮到你
我有以下 class:
public final class FileNames
{
private FileNames()
{}
public static final String A = "a.csv";
public static final String B = "b.csv";
public static final String C = "123.csv";
...
}
现在我想从上面获取所有 String 值,在不同的某处 class.Output 可以作为 String 数组或 ArrayList 或任何其他 Collection。我正在使用 jdk 1.7。不知道如何继续这个,因为可以增加字符串列表,即可以添加 "D"、"E" 等..
反思是你的朋友:)
public class Sample {
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
Class<FileNames> clazz = FileNames.class;
Field[] arr = clazz.getFields(); // Get all public fields of your class
for (Field f : arr) {
if (f.getType().equals(String.class)) { // check if field is a String
String s = (String)f.get(null); // get value of each field
// add s to a List
System.out.println(s);
}
}
}
}
final class FileNames {
private FileNames() {
}
public static final String A = "a.csv";
public static final String B = "b.csv";
public static final String C = "123.csv";
}
O/P :
a.csv
b.csv
123.csv
你添加这个功能:
public static ArrayList<String> getFilenames() {
ArrayList<String> filenames = new ArrayList<String>;
filenames.add(A);
filenames.add(B);
filenames.add(C);
return filenames;
}
然后你可以在某处使用这个函数:
ArrayList<String> filenames = new ArrayList<String>;
filenames = Filenames.getFilenames();
String filenameA = filenames.get(0); // to retrieve the first element (A)
String filenameB = filenames.get(1); // to retrieve the second element (B)
// and so on...
如果你想添加更多并以自动方式转换它,你可以检查这个:Java method with unlimited arguments
希望能帮到你