如何将arraylist中的值返回到屏幕上,例如System.out.println
我在想一个for循环,例如
for (String s : list)
{
System.out.println(s);
}
Run Code Online (Sandbox Code Playgroud)
但它抱怨它无法将对象转换为字符串 - 如何将其转换为字符串?
码:
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) {}
}
Run Code Online (Sandbox Code Playgroud)
如果您的问题是在for循环中打印,那么应该声明您的列表
ArrayList<String> list = new ArrayList<String>();
Run Code Online (Sandbox Code Playgroud)
并且for循环需要看起来像
for (String s : list)
{
System.out.println(s);
}
Run Code Online (Sandbox Code Playgroud)