简体   繁体   English

在两个不同的数组中找到3个相等的连续数字。 爪哇

[英]Finding 3 equal consecutive numbers in two different Arrays. Java

I am writing a program that reads a CSV file and stores each column into a separate array. 我正在编写一个程序,该程序读取CSV文件并将每一列存储到单独的数组中。 later I want to search arrays A and B and make for 3 consecutive 0s and then 2 ones following them. 稍后我想搜索数组A和B,并使其连续3个,然后跟随2个。 An example of the console after I run the program is; 我运行程序后的控制台示例是:

                                            A   B
Line # 1    1.3167  1.3164  1.318   1.3174  0   0
Line # 2    1.3167  1.3164  1.318   1.3174  0   0
Line # 3    1.3175  1.3164  1.3182  1.3169  0   0
Line # 4    1.3168  1.3167  1.3225  1.3212  1   1
Line # 5    1.3213  1.3206  1.3221  1.321   1   0
Line # 6    1.3211  1.3208  1.3241  1.3239  1   1
Line # 7    1.324   1.3237  1.3262  1.3242  1   1
Line # 8    1.3243  1.3234  1.3271  1.3245  0   1
Line # 9    1.3244  1.3223  1.3251  1.324   0   0
Line # 10   1.3241  1.3226  1.3269  1.3269  1   1

There are many more lines, however I want the program to find and a message to me every time the pattern from line 1-4 occur. 还有更多的行,但是每当第1-4行的模式出现时,我都希望程序查找并向我发送一条消息。

My code is 我的代码是

package pacakge;
import java.io.*;
import java.util.Scanner;

public class read {
  public static void main(String[] args) throws FileNotFoundException  {
    int size=0;
    double[] open = new double[1000000];
    double[] low = new double[1000000];
    double[] high = new double[1000000];
    double[] close = new double[1000000];
    int[] A = new int[1000000];
    int[] B = new int[1000000];

    int zeroCount = 0;

    Scanner read = new Scanner(new File("C:/Users/Eric/Documents/Data Structures/EURUSD.csv"));
    read.nextLine();
    while(read.hasNextLine()) {
      String[] n= read.nextLine().split(",");
      open[size]=Double.parseDouble(n[0]);
      low[size]=Double.parseDouble(n[1]);
      high[size]=Double.parseDouble(n[2]);
      close[size]=Double.parseDouble(n[3]);
      A[size]=Integer.parseInt(n[4]);
      B[size]=Integer.parseInt(n[5]);
      size++;
    }

    for (int i = 0; i < size; i++){
      System.out.println(open[i] + "\t" + low[i] + "\t" + high[i] + "\t" + close[i] + "\t" + A[i] + "\t" + B[i]);
      System.out.println("Line" + i);
      System.out.println(open[68722]);

      if(A[i]==0 && A[i+1] == 0 && A[i+2] == 0 && B[i]==0 && B[i+1] == 0 && B[i+2] == 0) {
        zeroCount++;
        System.out.println("Found Pattern !!!" + zeroCount);
        break;
      }
    }
  }
}

After I run it runs fine, but it finds three 0s however, the order isn't consecutive. 运行后,它运行正常,但找到三个0,但是顺序不是连续的。 I'm not sure why this is not working. 我不确定为什么这不起作用。 Thank you. 谢谢。

A simpler and perhaps more efficient solution to your problem is to convert these arrays A and B you have into String Astring and Bstring where each character in the string is an element in your array: ie if 一个更简单,也许更有效的解决方案是将您拥有的这些数组A和B转换为String Astring和Bstring,其中字符串中的每个字符都是数组中的一个元素:即

A={0,0,0,1,1,1,1,0,...};

Then Astring is: 那么Astring是:

Astring="00011110...";

Once you have this, finding what you are looking for is achieved in one line: 一旦有了这些,就可以在一行中找到您要寻找的东西:

int index = Astring.indexOf("00011");

if index=-1, you do not have the sequence 0,0,0,1,1 you were looking for. 如果index = -1,则您没有要查找的序列0,0,0,1,1。 Otherwise, A(index) will be the first element in the pattern you were looking for (ie A(index)=0, A(index+1)=0, ..., A(index+4)=1). 否则,A(index)将是您要寻找的模式中的第一个元素(即A(index)= 0,A(index + 1)= 0,...,A(index + 4)= 1)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM