-1

嗨,我正在尝试通过objectAobjectB并且我从方法ListA()和方法创建ListB()DoSomething有人可以指导我如何做到这一点吗?

public static void main(String[] args) 
{
myclass test = new myclass();
test.ListA(args[0]);
test.ListB(args[1]);            
test.DoSomething(objectA, objectB);
}

public void ListA(String aaa){
objectA = Sting[];
//other codes goes here...
}   

public void ListB(String bbb){
objectB = Sting[];
//other codes goes here...
}

public static void DoSomething(List<String>objectA, List<String>objectB}{
//other codes goes here...
}
4

3 回答 3

1

你为什么不直接返回你创建的对象来使用它DoSomething呢?

嗨,我正在尝试将我从方法 ListA() 和 List B() 创建的 objectA 和 objectB 传递给 DoSomething 方法,有人可以指导我如何做到这一点吗?

public static void main(String[] args) 
{
myclass test = new myclass();
List<String> objectA = test.ListA(args[0]);
List<String> objectB = test.ListB(args[1]);         
test.DoSomething(objectA, objectB);
}

public List<String> ListA(String aaa){
List<String> generatedA;
//Generate your object...
return generatedA;
}   

public List<String> ListB(String bbb){
List<String> generatedB;
//Generate your object...
return generatedB;
}

public static void DoSomething(List<String>objectA, List<String>objectB}{
//other codes goes here...
}
于 2013-04-27T11:37:52.907 回答
1

您将希望ListAandListB方法返回它们的值,然后将它们传递给DoSomething方法。

这里是关于 Java 方法的初学者教程:homeandlearn 和这里:Java Doc

于 2013-04-27T11:38:58.520 回答
0

为了能够通过它们,您应该返回:

public static void main(String[] args) 
{
    myclass test = new myclass();    
    test.DoSomething(test.ListA(args[0]), test.ListB(args[1]));
}

public List<String> ListA(String aaa){
    objectA = String[];
    //other codes goes here...
}   

public List<String> ListB(String bbb){
    objectB = String[];
    //other codes goes here...
}

public static void DoSomething(List<String>objectA, List<String>objectB}{
    //other codes goes here...
}

或者让他们成为成员:

List<String> objectA;
List<String> objectB;

public static void main(String[] args) 
{
myclass test = new myclass();
test.ListA(args[0]);
test.ListB(args[1]);        
test.DoSomething();
}

public List<String> ListA(String aaa){
this.objectA = Sting[];
//other codes goes here...
}   

public List<String> ListB(String bbb){
this.objectB = String[];
//other codes goes here...
}

public static void DoSomething(}{
   this.objectA
   this.objectB
//other codes goes here...
}
于 2013-04-27T11:44:51.820 回答