如何获取列表列表中的某个元素?
How to get a certain element in a list of lists?
我希望能够获取列表中的特定元素。但是,该列表包含在另一个列表中。这样做的原因是,我读入了一个属性文件,其中的键被分配了多个值,并用逗号分隔。我为每个键及其值创建一个列表,并将其添加到另一个列表,该列表包含每个键及其值的多个列表。
# Menu properties file
menuEntryPoint=0
indent=\t\t\t
lines=---------------------------------------------------------
mainMenu=M a i n M e n u
0.0=First Sub Menu, openMenu(1)
0.1=Second Sub Menu, openMenu(2)
0.2=Third Sub Menu, openMenu(3)
1=First Sub Menu
1.0=Execute this command, executeCommand(...)
1.1=Execute another command, executeCommand(...)
1.2=Execute other command, openMenu(A)
2=First Sub Menu
2.0=Execute this command, executeCommand(...)
2.1=Execute another command, executeCommand(...)
2.2=Execute other command, openMenu(A)
3=First Sub Menu
3.0=Execute this command, executeCommand(...)
3.1=Execute another command, executeCommand(...)
3.2=Execute other command, openMenu(A)
下面是我编写的用于将其打印到屏幕的代码:
List<List<String>> menuProperties = new ArrayList<List<String>>();
public void configMenuDisplayProperties() throws IOException {
Date date = new Date();
Properties menuProp = new Properties();
FileInputStream in = new FileInputStream("C:\Test\menu.properties");
menuProp.load(in);
//Create list of lists for properties
menuProperties.add(getPropertyList(menuProp, "0"));
menuProperties.add(getPropertyList(menuProp, "1"));
menuProperties.add(getPropertyList(menuProp, "2"));
menuProperties.add(getPropertyList(menuProp, "3"));
System.out.println(date.toString() + "\t\t" + menuProp.getProperty("developmentArea") + "\n\n");
System.out.println(menuProp.getProperty("indent") + menuProp.getProperty("mainMenu") + "\n");
for (Enumeration<?> e = menuProp.propertyNames(); e.hasMoreElements(); ) {
String name = (String)e.nextElement();
String value = menuProp.getProperty(name);
if (name.startsWith(menuProp.getProperty("menuEntryPoint"))) {
System.out.println(menuProp.getProperty("indent") + name + "\t" + menuProp.getProperty(name));
}
}
}
输出:
Mon Jun 08 13:39:43 EDT 2015
M a i n M e n u
0.2 Third Sub Menu, openMenu(C)
0.1 Second Sub Menu, openMenu(2)
0.0 First Sub Menu, openMenu(1)
我怎样才能打印出列表列表中某个键的特定值?我试图让我的输出看起来像这样,但我在这样做时遇到了麻烦。
Mon Jun 08 13:39:43 EDT 2015
M a i n M e n u
0.0 First Sub Menu
0.1 Second Sub Menu
0.2 Third Sub Menu
我尝试执行以下操作
System.out.println(menuProp.getProperty("indent") + name + "\t" + menuProp.getProperty(name));
但是我得到了你看到的第一个输出...任何帮助将不胜感激。
考虑使用 HashMap
:
HashMap<String, ArrayList<String>> menuProperties = new HashMap();
您可以将您的属性存储在地图中(键、值列表):
menuProperties.put("key", mylistofvalues);
您可以使用 get()
得到 属性:
ArrayList<String> tempList = menuProperties.get("mykey");
检查下面基于快速代码的解决方案,没有这样的基于 API 的解决方案可以满足您的要求。
如果要获取所有第一个逗号值,则使用 HashMap<String, String> indexedHashMap = getValueBashedOnIndex(hashMap, 1);
,如果要获取第三个逗号值,则使用 HashMap<String, String> indexedHashMap = getValueBashedOnIndex(hashMap, 3);
基本上 getValueBashedOnIndex
的第二个参数是你想要的索引值。
@SuppressWarnings("rawtypes")
private static void readPropertiesFile() {
ResourceBundle rb = ResourceBundle.getBundle("com.cgi.tmi.utils.test");
Enumeration<String> keys = rb.getKeys();
HashMap<String, String[]> hashMap = new HashMap<String, String[]>();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
String value = rb.getString(key);
//System.out.println(key + ": " + value);
hashMap.put(key, value.split(","));
}
HashMap<String, String> indexedHashMap = getValueBashedOnIndex(hashMap, 3);
Iterator<Entry<String, String>> it = indexedHashMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
}
}
@SuppressWarnings("rawtypes")
private static HashMap<String, String> getValueBashedOnIndex(HashMap<String, String[]> hashMap, int i) {
HashMap<String, String> indexedHashMap = new HashMap<String, String>();
Iterator<Entry<String, String[]>> it = hashMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
String[] strArr = (String[]) pair.getValue();
String value = "";
try {
value = strArr[(i-1)];
} catch (Exception e) {
}
indexedHashMap.put((String) pair.getKey(), value);
}
return indexedHashMap;
}
test.properties
menuEntryPoint=0
indent=\t\t\t
lines=---------------------------------------------------------
mainMenu=M a i n M e n u
0.0=First Sub Menu, openMenu(1)
0.1=Second Sub Menu, openMenu(2)
0.2=Third Sub Menu, openMenu(3)
1=First Sub Menu
1.0=Execute this command, executeCommand(...)
1.1=Execute another command, executeCommand(...)
1.2=Execute other command, openMenu(A)
2=First Sub Menu
2.0=Execute this command, executeCommand(...)
2.1=Execute another command, executeCommand(...)
2.2=Execute other command, openMenu(A)
3=First Sub Menu
3.0=Execute this command, executeCommand(...)
3.1=Execute another command, executeCommand(...)
3.2=Execute other command, openMenu(A)
test=test1,test2,test3,test1
我希望能够获取列表中的特定元素。但是,该列表包含在另一个列表中。这样做的原因是,我读入了一个属性文件,其中的键被分配了多个值,并用逗号分隔。我为每个键及其值创建一个列表,并将其添加到另一个列表,该列表包含每个键及其值的多个列表。
# Menu properties file
menuEntryPoint=0
indent=\t\t\t
lines=---------------------------------------------------------
mainMenu=M a i n M e n u
0.0=First Sub Menu, openMenu(1)
0.1=Second Sub Menu, openMenu(2)
0.2=Third Sub Menu, openMenu(3)
1=First Sub Menu
1.0=Execute this command, executeCommand(...)
1.1=Execute another command, executeCommand(...)
1.2=Execute other command, openMenu(A)
2=First Sub Menu
2.0=Execute this command, executeCommand(...)
2.1=Execute another command, executeCommand(...)
2.2=Execute other command, openMenu(A)
3=First Sub Menu
3.0=Execute this command, executeCommand(...)
3.1=Execute another command, executeCommand(...)
3.2=Execute other command, openMenu(A)
下面是我编写的用于将其打印到屏幕的代码:
List<List<String>> menuProperties = new ArrayList<List<String>>();
public void configMenuDisplayProperties() throws IOException {
Date date = new Date();
Properties menuProp = new Properties();
FileInputStream in = new FileInputStream("C:\Test\menu.properties");
menuProp.load(in);
//Create list of lists for properties
menuProperties.add(getPropertyList(menuProp, "0"));
menuProperties.add(getPropertyList(menuProp, "1"));
menuProperties.add(getPropertyList(menuProp, "2"));
menuProperties.add(getPropertyList(menuProp, "3"));
System.out.println(date.toString() + "\t\t" + menuProp.getProperty("developmentArea") + "\n\n");
System.out.println(menuProp.getProperty("indent") + menuProp.getProperty("mainMenu") + "\n");
for (Enumeration<?> e = menuProp.propertyNames(); e.hasMoreElements(); ) {
String name = (String)e.nextElement();
String value = menuProp.getProperty(name);
if (name.startsWith(menuProp.getProperty("menuEntryPoint"))) {
System.out.println(menuProp.getProperty("indent") + name + "\t" + menuProp.getProperty(name));
}
}
}
输出:
Mon Jun 08 13:39:43 EDT 2015
M a i n M e n u
0.2 Third Sub Menu, openMenu(C)
0.1 Second Sub Menu, openMenu(2)
0.0 First Sub Menu, openMenu(1)
我怎样才能打印出列表列表中某个键的特定值?我试图让我的输出看起来像这样,但我在这样做时遇到了麻烦。
Mon Jun 08 13:39:43 EDT 2015
M a i n M e n u
0.0 First Sub Menu
0.1 Second Sub Menu
0.2 Third Sub Menu
我尝试执行以下操作
System.out.println(menuProp.getProperty("indent") + name + "\t" + menuProp.getProperty(name));
但是我得到了你看到的第一个输出...任何帮助将不胜感激。
考虑使用 HashMap
:
HashMap<String, ArrayList<String>> menuProperties = new HashMap();
您可以将您的属性存储在地图中(键、值列表):
menuProperties.put("key", mylistofvalues);
您可以使用 get()
得到 属性:
ArrayList<String> tempList = menuProperties.get("mykey");
检查下面基于快速代码的解决方案,没有这样的基于 API 的解决方案可以满足您的要求。
如果要获取所有第一个逗号值,则使用 HashMap<String, String> indexedHashMap = getValueBashedOnIndex(hashMap, 1);
,如果要获取第三个逗号值,则使用 HashMap<String, String> indexedHashMap = getValueBashedOnIndex(hashMap, 3);
基本上 getValueBashedOnIndex
的第二个参数是你想要的索引值。
@SuppressWarnings("rawtypes")
private static void readPropertiesFile() {
ResourceBundle rb = ResourceBundle.getBundle("com.cgi.tmi.utils.test");
Enumeration<String> keys = rb.getKeys();
HashMap<String, String[]> hashMap = new HashMap<String, String[]>();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
String value = rb.getString(key);
//System.out.println(key + ": " + value);
hashMap.put(key, value.split(","));
}
HashMap<String, String> indexedHashMap = getValueBashedOnIndex(hashMap, 3);
Iterator<Entry<String, String>> it = indexedHashMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
}
}
@SuppressWarnings("rawtypes")
private static HashMap<String, String> getValueBashedOnIndex(HashMap<String, String[]> hashMap, int i) {
HashMap<String, String> indexedHashMap = new HashMap<String, String>();
Iterator<Entry<String, String[]>> it = hashMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
String[] strArr = (String[]) pair.getValue();
String value = "";
try {
value = strArr[(i-1)];
} catch (Exception e) {
}
indexedHashMap.put((String) pair.getKey(), value);
}
return indexedHashMap;
}
test.properties
menuEntryPoint=0
indent=\t\t\t
lines=---------------------------------------------------------
mainMenu=M a i n M e n u
0.0=First Sub Menu, openMenu(1)
0.1=Second Sub Menu, openMenu(2)
0.2=Third Sub Menu, openMenu(3)
1=First Sub Menu
1.0=Execute this command, executeCommand(...)
1.1=Execute another command, executeCommand(...)
1.2=Execute other command, openMenu(A)
2=First Sub Menu
2.0=Execute this command, executeCommand(...)
2.1=Execute another command, executeCommand(...)
2.2=Execute other command, openMenu(A)
3=First Sub Menu
3.0=Execute this command, executeCommand(...)
3.1=Execute another command, executeCommand(...)
3.2=Execute other command, openMenu(A)
test=test1,test2,test3,test1