怎样才能将数组列表中的值返回到屏幕上,例如: System.out.println输出数组列表的内容
我正在考虑for循环,例如
for (String s : list)
{
System.out.println(s);
}
但它抱怨它不能将一个对象转换为字符串 - 我如何将它转换为字符串?
代码:
public static void GetStatsProc(String operation)
{
try {
Process p=Runtime.getRuntime().exec(operation);
p.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line=reader.readLine();
while(line!=null)
{
// Add all lines into an array
ArrayList list = new ArrayList();
list.add(line);
System.out.println(line);
line=reader.readLine();
}
}
catch(IOException e1) {}
catch(InterruptedException e2) {}
}
您应该使用通用'ArrayList'(即'ArrayList')。 –
Bernard
2012-02-01 15:50:58
没有人指出你正在while循环的每次迭代中声明一个新列表。 – Sign 2012-02-01 15:53:24
为什么你的示例代码不包含foreach循环?你想把它放在哪里? – Jim 2012-02-01 15:54:20