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

穿精又带淫゛_ 提交于 2019-12-13 09:44:34

问题


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.


回答1:


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.




回答2:


I believe you are trying to keep track of repeating co-ordinates, If yes, then this might help!

int i=0;
HashMap<Double,Double> hst = new HashMap<Double,Double>();
while(i<xcoordinate.length){

    if(hst.containsKey(xcoordinate[i])){
        if(hst.get(xcoordinate[i]) == ycordinate[i])
        System.out.println("Occured before and now again at pos" +i);
        i++;
        continue;
    }

    System.out.println("Not occured before");
    hst.put(xcoordinate[i],ycordinate[i]);
    i++;
}



回答3:


You need to change, you have a syntax error:

if((xcoordinate[i]==xcoordinate[k])&&(ycordinate[i]==ycordinate[k]))

If expressions don't evaluate as you coded.

EDIT 2: Here is the full workable code:

 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};
    for(int i = 0; i < xcoordinate.length; i++)
    for(int k = i+1; k < xcoordinate.length; k++)
            if(xcoordinate[i]==xcoordinate[k]&&ycordinate[i]==ycordinate[k]){
            System.out.println("Both co ordinates are same: " + i + "," + k);
        }
        else{
            System.out.println("They are not same" + i + " " + k);
        }
    }

EDIT 3: The same functionality, using while loops, the correct way of doing:

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;
    while(i < xcoordinate.length)
    {
        int k = i+1;
        while(k < xcoordinate.length)
        {
        if(xcoordinate[i]==xcoordinate[k]&&ycordinate[i]==ycordinate[k]){
            System.out.println("Both co ordinates are same: " + i + "," + k);
        }
        else{
            System.out.println("They are not same" + i + " " + k);
        }
        k++;
        }
        i++;
    }
}


来源:https://stackoverflow.com/questions/51787217/array-pairwise-matching-in-java-give-error-also-store-data-between-two-similar-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!