简体   繁体   English

如何将 object 数组传递给另一个 class 中的方法,以便此方法将其用作普通数组?

[英]How can I pass an object array to a method in another class so this method uses it as a normal array?

I am pretty new to java and programming in general so sorry if I confuse some concepts, in my code I have an object called cr which is an array in the class Menu constructed from the class ControllerRoute , this instance I require to pass to a method in the class UpdateAndDelete that does the process of update and delete of a CRUD, but the code in the method doesn't read as an array but as an object ControllerRoute .我对 java 和一般编程很陌生,所以很抱歉,如果我混淆了一些概念,在我的代码中我有一个名为cr的 object,它是 class 菜单中的一个数组,该菜单由 class ControllerRoute构造,我需要将此实例传递给一个方法在执行 CRUD 更新和删除过程的 class UpdateAndDelete中,但该方法中的代码不是作为数组读取,而是作为 object ControllerRoute读取。 I did try having different names in the parameter and using super.cr but it didn't work for me.我确实尝试在参数中使用不同的名称并使用super.cr但它对我不起作用。

The Menu is to use the different actions of the CRUD, but here is the update part, which is the one giving problems. Menu 是使用 CRUD 的不同操作,但这里是更新部分,这是给出问题的部分。

import javax.swing.JOptionPane;
public class Menu{
private ControllerRoute cr = new ControllerRoute(100);
UpdateAndDelete ud = new UpdateAndDelete();

   public void updateRoute(){
      ControllerRoute ur = new ControllerRoute(0);
      int id;
      id = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter an Id"));
      ud.updateRoutes(id, cr);
      }
}

Route alongside ControllerRoute are the classes used to create the array and from where the object cr comes. Route 与 ControllerRoute 一起是用于创建数组的类,object cr来自于此。

public class Route{
   private int id;
   private String name;
   
   public Route () {}
   
   public Route(int id, String name){
      this.id = id;
      this.name = name;
   }
}
public class ControllerRoute extends Menu{
   Route[] routes;
   
   public ControllerRoute(int size){
      routes = new Route[size];
   }
}

UpdateAndDelete is the method that is asking for cr to do the process of update and delete parts of the array. UpdateAndDelete 是要求cr执行更新和删除数组部分的过程的方法。 Here is where in every place that has cr in it an error appears because either it can't found the symbol nor can recognize cr as an array.这是在每个有cr的地方出现错误的地方,因为它要么找不到符号,要么不能将 cr 识别为数组。

import javax.swing.JOptionPane;
public class UpdateAndDelete{

   public Route updateRoutes(int id, ControllerRoute cr){
      int pos = -1;
      String nwname;
      for(int i=0; i<cr.length; i++){
           if(id == cr[i].getId()){
            pos=i;
           }
      }
      return null;
   }
}

Errors found:发现错误:

error: cannot find symbol on the line: for(int i=0; i<cr.length; i++){错误:在行中找不到符号:for(int i=0; i<cr.length; i++){

UpdateAndDelete.java:14: error: array required, but ControllerRoute found in the lines: if(id == cr[i].getId()){ UpdateAndDelete.java:14:错误:需要数组,但在以下行中找到 ControllerRoute :if(id == cr[i].getId()){

cr[pos] = new Route(id, nwname); cr[pos] = new Route(id, nwname);

I first tried changing the parameter, either the name of the array or the type alongside it between int, String, Array.我首先尝试更改参数,要么是数组的名称,要么是 int、String、Array 之间的类型。 Route[] and the final one being ControllerRoute. Route[] 最后一个是 ControllerRoute。

After that I tried using super.cr to see if I could take it directly from menu, but it gave problems with methods like.length之后我尝试使用 super.cr 看看我是否可以直接从菜单中获取它,但是它给像.length 这样的方法带来了问题

import javax.swing.JOptionPane;
public class UpdateAndDelete extends Menu{

   public Route updateRoutes(int id, ControllerRoute cr){
      int pos = -1;
      String nwname;
      for(int i=0; i<super.cr.length; i++){
           if(id == super.cr[i].getId()){
            pos=i;
           }
      }
      return null;
   }
}

This gives the same errors.这给出了相同的错误。

Other thing that I tried with the super was trying create another array in UpdateAndDelete and use super.cr to translate it's value to it ( example: pj = super.cr ), but it gave out the error that the array wasn't compatible with ControllerRoute我用 super 尝试的另一件事是尝试在UpdateAndDelete中创建另一个数组并使用 super.cr 将它的值转换为它(例如:pj = super.cr ),但它给出了数组不兼容的错误控制器路由

ControllerRoute is not an array. ControllerRoute 不是数组。 it has an attribute which is array.它有一个属性是数组。 try to write a getter for routes in ControllerRoute and use that to get the array.尝试在 ControllerRoute 中为路由编写一个 getter 并使用它来获取数组。 ud.updateRoutes(id, cr.getRoutes()); ud.updateRoutes(id, cr.getRoutes());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM