Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

if statement - Array pairwise matching in java give error also store data between two similar element

I have two array. They are represent x-coordinates and y-coordinates. The code is like that if both coordinates are same it print a statement "Both co ordinates are same" else they print They are not same.

My code is

 public static void main(String args[]){
    double[]xcoordinate={2.3,1.2,3.3,5.5,2.3,1.3,7.9,1.2,3.3,3.3,5.2};
    double[]ycordinate={5.4,2.2,4.4,6.6,5.4,1.9,5.2,2.2,3.5,4.4,4.2};
    int i=0,k=1;
    while(i<xcoordinate.length){
        //if(xcoordinate[i]&&ycordinate[i]==xcoordinate[k]&&ycordinate[k]){
        if(xcoordinate[i]==xcoordinate[k]&&ycordinate[i]==ycordinate[k]){
            System.out.println("Both co ordinates are same");
            i++;
            if(k<xcoordinate.length) {
                k = i + 1;
            }

        }
        else{
            System.out.println("They are not same");
        }
        k++;
    }
}

If I analysis the array I can see that 2.3 and 5.4 are pair,1.2 and 2.2 are pair and so on. As you can see 2.3 and 5.4 is repeat at 5th pair. So in this time they print They are same.

But the code block not run for that if statement.

Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11

How to eliminate this error and also how to store the They are not same types of data?

Like want to store{ {1.2,3.3,5.5}and{2.2,4.4,6.6} }and {{3.3,5.5,2.3,1.3,7.9}and{4.4,6.6,5.4,1.9,5.2}} ..... separately.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Apart from the answer from Simion who is solving your evaluation problem, will you end up having an enless loop, cause you only increment i (i++) when your numbers are the same.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...