如何将数组变量从一个方法调用到另一个变量
如何将 Attribute() 方法中的变量 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]);
}
}
How do I call the variable show[j]
and actuate[j]
which are located in Attribute() method into xmls(). If I declare this outside I will get ArrayIndexOutOfBoundException
. count is a variable got from other query.
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]);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您不“调用”变量。您调用方法和构造函数 - 它要求它们做一些事情。你不能用变量来做到这一点。
至于如何从
xmlS
方法访问变量,有两个直接选项:xmlS
(如果您可以从声明它们的方法调用该方法)。 (您没有在所显示的代码中这样做,但您可能在实际代码中这样做。)您的类要做什么并不明显(并且方法名称无助于揭示任何内容)要么),所以不清楚哪个实际上是合适的。如果它们在逻辑上是对象状态的一部分,则将它们设为实例变量。否则,请考虑数据应如何流通过您的程序。也许应该从
Attribute
方法返回此数据吗? (例如,作为显示/执行对的List
)Firstly, you don't "call" variables. You call methods and constructors - it's asking them to do something. You don't do that with variables.
As for how you can get access to your variables from the
xmlS
method, there are two immediate options:xmlS
, if you can call that method from the method where they're declared. (You're not doing so in the code you've shown, but you may be doing so in your real code.)It's not obvious what your class is meant to be doing (and the method names don't help to reveal anything either), so it's unclear which is actually appropriate. If they're logically part of the state of the object, then make them instance variables. Otherwise, consider how the data should flow through your program. Should this data be returned from the
Attribute
method perhaps? (e.g. as aList
of show/actuate pairs)你不能。它们是
Attribute()
中的局部变量,因此它们仅在您调用Attribute()
时才存在,并且您永远不会从Attribute()
中调用xmlS()
。您必须从Attribute()
中调用xmlS()
并将它们作为参数传递给它。You can't. They're local variables in
Attribute()
, so they only exist as long as you're in a call toAttribute()
, and you never callxmlS()
from withinAttribute()
. You'd have to callxmlS()
from withinAttribute()
and pass them into it as arguments.