首页 > 解决方案 > 如何正确同步类之间的向量以避免使用静态?

问题描述

我有一个读取向量向量的类,它甚至包含一个字符串名称和包含玩家“投注”的内部向量。

但是,我在阅读向量中使用静态,如果我删除静态这个词,当在比较类中向量始终为零时。(我知道我在比较类中给出了一个新对象,它与之前填充的向量无关)

我想更改代码以同步 3 个类之间的向量,以便我可以进行正确的参数传递,但我不知道该怎么做。

您能否向我解释一下如何正确同步类之间的向量?

请记住,它不仅仅是向量,它们是由字符串名称 + 下注向量组成的下注类型对象。

此类 readTemplate 有一个静态声明: public static int[] templateVector= new int [13];

CLASS readTemplate - 部分代码:

public void readTemplate() throws NumberFormatException, IOException {
        
    File templatesFile= new File ("C:\\Temp\\templatesFile.txt");
    FileReader fr = new FileReader(templatesFile);
    BufferedReader br = new BufferedReader(fr);
        
    String row;
        
    int j = 0;
        
    while((row= br.readLine()) != null) {
        String[] strings = row.split(" ");
        for (String str : strings) {
             Integer foo = Integer.parseInt(str);
             templatevector[j] = foo;
             j++;
        }       
    }
        
    br.close();
    fr.close();
}

部分代码,仅用于理解目的:

类 generateResults(将此模板文件与投注进行比较以找到获胜者)

public void generateResult() throws IOException {
    
    ReadTemplate Template = new ReadTemplate();
    ReadBets a = new ReadBets();
    
    File output = new File ("C:\\Temp\\output.txt");
    
    FileWriter fw = new FileWriter("c:\\Temp\\output.txt");
    BufferedWriter bw = new BufferedWriter(fw);
    
    bw.write("TEMPLATE READ:");
    for (int j = 0; j < template.vectorTemplate.length; j++) {
        bw.write(template.vectorTemplate[j] + " ");
    }
    
    bw.newLine();
    bw.write("+--Result Bets:--+");
    bw.newLine();
    
    try {
        if (!output.exists()) {

        output.createNewFile();
    }
    
    for(int j = 0; j < size; j++) {
        int i = 0;
        int counter = 0;
    
        for(int x = 0; x < template.vectorTemplate.length; x++) {
            if (a.totalStakes[j].vectorStakes[i] == template.vectorTemplate[x]) {
                counter++;
            }
        i++;
    }
}

非常感谢任何花时间向我解释的人。

标签: javastringvectorstatic

解决方案


推荐阅读