Java 如何将数组变量从一个方法调用到另一个变量中
如何将位于Attribute()方法中的变量Java 如何将数组变量从一个方法调用到另一个变量中,java,variables,jdbc,Java,Variables,Jdbc,如何将位于Attribute()方法中的变量show[j]和actuate[j]调用到xmls()。如果我在外部声明,我将获得ArrayIndexOutOfBoundException。count是从其他查询中获取的变量 void Attribute() throws SQLException{ Statement statement3=connection.createStatement(); String Querystring3="select Show ,actuate fr
show[j]
和actuate[j]
调用到xmls()。如果我在外部声明,我将获得ArrayIndexOutOfBoundException
。count是从其他查询中获取的变量
void Attribute() throws SQLException{
Statement statement3=connection.createStatement();
String Querystring3="select Show ,actuate from rlink";
ResultSet Attrib=statement3.executeQuery(Querystring3);
String[] Show=new String[Count];
String[] Actuate=new String[Count];
while(Attrib.next()){
Show[j]=Attrib.getString(1);
Actuate[j]=Attrib.getString(2);
j++;
}
for(i=0;i<Count;i++){
System.out.println(Show[i]+" "+Actuate[i]);
}
}
void xmlS() throws IOException{
Element child = doc.createElement("body");
root.appendChild(child);
for(i=0;i<LinkCount;i++){
Element child1 = doc.createElement("link");
child1.setAttributeNS(xlink,"xlink:show", Show[i]);
child1.setAttributeNS(xlink,"xlink:actuate",Actuate[i]);
}
}
void属性()引发SQLException{
语句statement3=connection.createStatement();
String Querystring3=“选择显示,从rlink启动”;
ResultSet Attrib=statement3.executeQuery(Querystring3);
字符串[]显示=新字符串[计数];
字符串[]执行=新字符串[计数];
while(Attrib.next()){
Show[j]=Attrib.getString(1);
Actuate[j]=Attrib.getString(2);
j++;
}
对于(i=0;i你不能。它们是Attribute()
中的局部变量,所以只要你调用Attribute()
,它们就存在,而你永远不会从Attribute()
中调用xmlS()
。你必须从Attribute()中调用xmlS()
并将它们作为参数传递给它。首先,你不“调用”变量。你调用方法和构造函数-它要求他们做一些事情。你不使用变量
至于如何从xmlS
方法访问变量,有两个直接选项:
- 使它们成为实例变量而不是局部变量,即将它们声明为类的成员
- 如果可以从声明它们的方法中调用该方法,请将它们作为参数传递给
xmlS
。(您在显示的代码中没有这样做,但可能在实际代码中这样做。)
你的类要做什么并不明显(方法名也无助于揭示任何东西)因此,不清楚哪一个实际上是合适的。如果它们在逻辑上是对象状态的一部分,那么将它们作为实例变量。否则,考虑数据应该如何流过程序。这些数据是否应该从<代码>属性< /代码>方法中返回?(例如,作为显示/驱动对的<代码>列表< /代码>)@user977830:我建议你买一本关于学习Java的书。虽然实验和提问很好,但你真的应该从从头开始教Java的东西开始。堆栈溢出并不能很好地替代它。