【问题标题】:Store output from the Main to a Class将输出从 Main 存储到 Class
【发布时间】:2013-08-02 15:50:35
【问题描述】:

这是我在Main 方法中的代码的一部分。我想将一个特定数组的输出存储在一个新类中(你可以在代码的注释中看到它。

    int splitNum = 0;
    BufferedWriter out = 
       new BufferedWriter(new OutputStreamWriter(new FileOutputStream("data.txt"), "ISO-8859-1"));

    ArrayList<Split> allSplits = new ArrayList<Split>();

    int[]version=new int[(int) NUM_OF_SPLITS];
    while((numRead = (reader.read(buf))) != -1){
       String readData = String.valueOf(buf, 0, numRead);

                   version[splitNum]=1;

     //     I need to store the output of the array version in a new class             


       String encrypted = Encryptor.run(readData);
       Split split = new Split(encrypted, splitNum);
       System.out.println( "Split:" + split.blocks + "\n\n" );
       WatchDog.run(split);
       Permutator.run(split);
       System.out.println( "Final Split:" + split.blocks + "\n\n");
      allSplits.add(split);


       buf = new char[StaticData.splitSize];
       splitNum++;
    }

这似乎是一些基本的东西,但我现在想不起来。有什么帮助吗?谢谢

【问题讨论】:

  • “新课程”是什么意思?
  • 我的意思是现在的输出在 MainClass 中,我想将它复制到 Main 之外的另一个独立类中,例如 Version

标签: java arrays class output


【解决方案1】:

定义一个名为 ArrayStore 的类

在该类中声明一个 ArrayList。

public class ArrayStore
{
private ArrayList list1;

public ArrayStore()
{
this.list1 = new ArrayList();
}

void addInList(String text)
{
    this.list1.add(text);
}

ArrayList getList()
{
    return this.list1;
}
}

在循环之前实例化该类及其数组列表。

ArrayStore store= new ArrayStore();

将阅读器生成的元素添加到此列表中。

while((numRead = (reader.read(buf))) != -1){
       String readData = String.valueOf(buf, 0, numRead);

                   version[splitNum]=1;

//类添加代码从这里开始

     store.addInList(readData);

就是这样。

【讨论】:

  • 谢谢,但我不想存储 readData 但 version[] 所以我需要保留数组列表还是应该更改类型?
猜你喜欢
  • 1970-01-01
  • 2016-01-12
  • 2012-04-12
  • 1970-01-01
  • 1970-01-01
  • 2018-05-28
  • 1970-01-01
  • 2014-07-10
  • 2022-01-09
相关资源
最近更新 更多