开发者

Getting the results of my Treeset in another method of the same class

I've created a Treeset of a textfile that was read into my fileRead method and I want to pass the results to my fileWrite method. The two methods are in the same cla开发者_如何学Pythonss. How do i retrieve the treeset results from the fileWrite method?


Make your fileRead method return the TreeSet, store it in a variable and then pass that to the fileWrite method.

TreeSet ts = fileRead();
fileWrite( ts );


You can save the treeset results as a member variable and then access that variable in the fileWrite method. Or, you could have the fileRead method return the treeset results, and then leave it up to the user to then pass the results into the fileWrite method.

And just to clarify, a member variable is a non-static (i.e. one per instance) variable declared in the class that contains your fileRead and fileWrite methods.

For example:

public class MyClass {
 private TreesetResults results;

 public void fileRead(...) {
  // Determine results, then save them to member variable
  results = determinedResults;
 }

 public void fileWrite(...) {
  // Access results from fileRead and do something with them
  useResults( results );
 }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜