将arrayList的值从void方法传递到另一个类中的arrayList

[英]Passing value of arrayList from a void method to arrayList in another class


I have a class named Preprocessing and has a method called Process()

我有一个名为Preprocessing的类,并有一个名为Process()的方法

, it has an ArrayList called datatweets that contains my data.

,它有一个名为datatweets的ArrayList,它包含我的数据。

public void Process(){
 //the process...
   datatweets.add(mydata);
}

then I try to pass the datatweets and the value(my data) to another ArrayList on the different class. I've tried using getter method such as:

然后我尝试将datatweets和值(我的数据)传递给另一个类上的另一个ArrayList。我尝试过使用getter方法,例如:

public ArrayList getMyList(){
       return datatweets;
} 

but it still doesn't print any value when I call it. please tell me what is wrong, and what should I do?

但是当我打电话时它仍然没有打印任何值。请告诉我有什么问题,我该怎么办?

here's what i do to call the arrayList:

这是我做的调用arrayList:

Preprocessing data = new Preprocessing();
        ArrayList<String> dataset = new ArrayList<>();
        dataset = data.getMyList();

        for(int a=0;a<dataset.size();a++){
            System.out.println(dataset.get(a));
        }

1 个解决方案

#1


1  

As i understand you have an class named "Preprocessing". Now you should have "datatweets" as your instance variable of the class.

据我所知,你有一个名为“预处理”的类。现在你应该有“datatweets”作为类的实例变量。

Now you should create an object of Preprocessing and call the method "Process". In the method process you should fill the arraylist "datatweets". Process method should contain below code.

现在,您应该创建一个Preprocessing对象并调用方法“Process”。在方法过程中,您应该填充arraylist“datatweets”。处理方法应包含以下代码。

if(this.dataTweets == null)
 this.dataTweets = new ArrayList<>();

this.dataTweets.add(myData);

Once the datatweets is filled by calling the process method. You should have getter method in same class which should return datatweets

通过调用process方法填充datatweets之后。你应该在同一个类中有getter方法,它应返回datatweets

public List<String> getDataTweets(){
return this.dataTweets;
}

Now the main function should look like this

现在主要功能应该是这样的

public static void main(String[] args){
Preprocessing preprocessor = new Preprocessing();
preprocessor.Process();
List<String> dataTweets = preprocessor.getDataTweets();
 //Now iterate over this you will surely get data.
}

You class Preprocessor should be like this

您的类预处理器应该是这样的

public class Preprocessor{
 private List<String> dataTweets;
 public void process(){
    //processing
   this.dataTweets.add(data);
 }
 public List<String> getDataTweets(){
   return this.dataTweets;
 }
}
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2017/12/15/3541cdee5c19f2cdd1fe12e79f3e8cf8.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告