如何将数组变量从一个方法调用到另一个变量

发布于 2024-12-09 13:07:59 字数 995 浏览 0 评论 0原文

如何将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还如梦归 2024-12-16 13:07:59

首先,您不“调用”变量。您调用方法和构造函数 - 它要求它们一些事情。你不能用变量来做到这一点。

至于如何从 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:

  • 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)

救星 2024-12-16 13:07:59

你不能。它们是 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文