我想知道如何返回在for循环中分配的数组变量的所有值。
在下面的方法中,我将值分配给Output数组。现在我想将Output数组中的所有值显示为return参数。由于范围级别,我得到了最后一个值。
现在,由于范围问题,我可以返回最后一个值。
public static String[] getMBeanAppsStatus(String host, String port,
String container, String filter,
String usr, String pwd) {
String Output[] = new String[3];
int mbeanAppsIdx = 0;
int LVar = mbeanAppsIdx;
try {
Object[] connections =
connectMethd(host, port, container, filter, usr, pwd);
MBeanServerConnection serverConn =
(MBeanServerConnection)connections[0];
System.out.println("serverConn from array variable in getMBeanAppsStatus" +
serverConn);
/* retrieve mbean apps matching given filter */
ObjectName mbeanDomainName = new ObjectName(filter);
Set mbeanAppNames = serverConn.queryNames(mbeanDomainName, null);
/* pattern to extract mbean application names */
Pattern mbeanAppPat =
Pattern.compile("name=", Pattern.CASE_INSENSITIVE);
/* mbean application name */
ObjectName mbeanApp = null;
/* retrieve mbean apps count */
Iterator i;
for (i = mbeanAppNames.iterator(); i.hasNext(); ) {
mbeanAppsIdx++;
System.out.println("Populating MBean App #" + mbeanAppsIdx +
"details...");
/* retrieve mbean app name and status */
mbeanApp = (ObjectName)i.next();
String mbeanAppFQDN = mbeanApp.toString();
String mbeanAppName = mbeanAppPat.split(mbeanAppFQDN)[1];
Boolean mbeanAppRunning =
Boolean.valueOf(serverConn.getAttribute(mbeanApp,
"Running").toString());
String mbeanAppStatus = "DISABLED";
if (mbeanAppRunning.booleanValue())
mbeanAppStatus = "ENABLED";
Output[0] = mbeanAppName;
Output[1] = mbeanAppFQDN;
Output[2] = mbeanAppStatus;
System.out.println("getMBeanAppsStatus output " +
"mbeanAppName " + mbeanAppName +
" mbeanAppFQDN " + mbeanAppFQDN +
" mbeanAppStatus : " + mbeanAppStatus);
}
} catch (MalformedObjectNameException e) {
e.getStackTrace();
} catch (InstanceNotFoundException e) {
e.getStackTrace();
} catch (AttributeNotFoundException e) {
e.getStackTrace();
} catch (ReflectionException e) {
e.getStackTrace();
} catch (MBeanException e) {
e.getStackTrace();
} catch (IOException ioe) {
System.out.println(ioe);
}
System.out.println("getMBeanAppsStatus Output " + Output);
return Output;
}
基本上,我正在尝试将上述方法转换为J2EE Web服务,并且来自Web服务的响应将是该方法的Output的返回值。
我在方法中有2个问题,我想纠正它。
1)想要将mbeanAppName
,mbeanAppFQDN
和mbeanAppStatus
的值与逗号,
分隔符连接起来并分配给数组变量。
2)返回应保存所有先前值的数组结果。
答案 0 :(得分:1)
您只看到最后一次输出,因为在每次迭代时,您都会用当前输出覆盖以前的输出。
您可以使用StringBuilder
连接以逗号mbeanAppName
分隔的mbeanAppFQDN
,mbeanAppStatus
和,
。这可以通过以下方式完成:
StringBuilder sb = new StringBuilder();
/* Declare and initialise variables somewhere in between*/
sb.append(mbeanAppName);
sb.append(",");
sb.append(mbeanAppFQDN);
sb.append(",");
sb.append(mbeanAppStatus);
String generatedStringOutputFromStringBuilder = sb.toString();
每次迭代后,您都可以使用List
存储输出。与数组不同,List
可以改变大小。当您初始化它时,您不必声明它。在每次迭代时将输出添加到创建的List
。例如:
List<String> output = new ArrayList<String>();
/* Declare and initialise variables somewhere in between*/
output.add(generatedStringOutputFromStringBuilder);
以下是应该将您设置在正确方向的更正样本。
public static List<String> getMBeanAppsStatus(String host, String port,
String container, String filter,
String usr, String pwd) {
// A new List
List<String> output = new ArrayList<String>();
//A StringBuilder that will be used to build each ouput String
StringBuilder sb = new StringBuilder();
int mbeanAppsIdx = 0;
int LVar = mbeanAppsIdx;
try {
Object[] connections = connectMethd(host, port, container, filter, usr, pwd);
MBeanServerConnection serverConn = (MBeanServerConnection)connections[0];
System.out.println("serverConn from array variable in getMBeanAppsStatus" + serverConn);
/* retrieve mbean apps matching given filter */
ObjectName mbeanDomainName = new ObjectName(filter);
Set mbeanAppNames = serverConn.queryNames(mbeanDomainName, null);
/* pattern to extract mbean application names */
Pattern mbeanAppPat = Pattern.compile("name=", Pattern.CASE_INSENSITIVE);
/* mbean application name */
ObjectName mbeanApp = null;
/* retrieve mbean apps count */
Iterator i;
for (i = mbeanAppNames.iterator(); i.hasNext(); ) {
mbeanAppsIdx++;
System.out.println("Populating MBean App #" + mbeanAppsIdx + "details...");
/* retrieve mbean app name and status */
mbeanApp = (ObjectName)i.next();
String mbeanAppFQDN = mbeanApp.toString();
String mbeanAppName = mbeanAppPat.split(mbeanAppFQDN)[1];
Boolean mbeanAppRunning = Boolean.valueOf(serverConn.getAttribute(mbeanApp, "Running").toString());
String mbeanAppStatus = "DISABLED";
if (mbeanAppRunning.booleanValue())
mbeanAppStatus = "ENABLED";
// Construct the output using a string builder as
// mbeanAppName,mbeanAppFQDN,mbeanAppStatus
sb.append(mbeanAppName);
sb.append(",");
sb.append(mbeanAppFQDN);
sb.append(",");
sb.append(mbeanAppStatus);
// Store the current ouput in the List output
output.add(sb.toString());
// Empty string builder
sb.setLength(0)
System.out.println("getMBeanAppsStatus output " +
"mbeanAppName " + mbeanAppName +
" mbeanAppFQDN " + mbeanAppFQDN +
" mbeanAppStatus : " + mbeanAppStatus);
}
} catch (MalformedObjectNameException e) {
e.getStackTrace();
} catch (InstanceNotFoundException e) {
e.getStackTrace();
} catch (AttributeNotFoundException e) {
e.getStackTrace();
} catch (ReflectionException e) {
e.getStackTrace();
} catch (MBeanException e) {
e.getStackTrace();
} catch (IOException ioe) {
System.out.println(ioe);
}
System.out.println("getMBeanAppsStatus Output " + output);
return output;
}