开发者

how to call array variable from one method into another variable

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";
  Result开发者_开发百科Set 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]);

       }
   }


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:

  • Make them instance variables instead of local variables, i.e. declare them as members of the class
  • Pass them as parameters to 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 a List of show/actuate pairs)


You can't. They're local variables in Attribute(), so they only exist as long as you're in a call to Attribute(), and you never call xmlS() from within Attribute(). You'd have to call xmlS() from within Attribute() and pass them into it as arguments.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜