简体   繁体   中英

How to compare two arrays of different sizes?

So my task is to read a file line by line and store the integers into an array. Then to add the integers in spots 1-5, 2-6, 3-7 etc. and store those into a new array.

In array 1 there is 4 more values than array 2. I need to compare these Arrays and see if array1 is 0.999 bigger than array2.

If it is indeed larger, I need to print out the LOCATION of the number in the array 1.

Right now my problem is my code is outputting that every number is larger than the corresponding number in array 2.

Code:

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public class Asgn7
{
    public static void main(String[] args) throws FileNotFoundException
    {   
        Scanner file = new Scanner(new File("asgn7data.txt"));
        double[] array = new double[file.nextInt()];
        double[] newArray = new double[array.length - 4];
        double tempVal = 0;
        int j = 0;
        int count = 0;

        while(file.hasNext())
        {
        for(int i = 0; i < array.length ; i++)
        {
            array[i] = file.nextInt();
        }

        for(j = 0; j < array.length - 4; j++)
        {
            for(int k = 0; k < 5; k++)
            {
                newArray[j] += array[j+k] / 5;  
             }

        }

        for(int i = 2; i < array.length; i++)
        {
            if(array[i] > (newArray[i-2] + 0.999));
            {
                count++;
                tempVal = count;
            }

             System.out.println(tempVal);
        }   
        }

    }
}

在此处输入图片说明

The values which should be compared are from 3-13.

Judging by the picture, you are not placing the values in the correct index in the second array, or you are not matching the correct ones.

If you want it to look exactly like in the picture, the second array should be declared:

double[] newArray = new double[array.length - 2];

And the loop to fill it should be changed to:

    for(j = 2; j < array.length - 2; j++)
    {
        for(int k = -2; k <= 2; k++)
        {
            newArray[j] += array[j+k] / 5;  
        }

    }

This will put the averages in the third, fourth, fifth... elements in newArray . And now you can compare them directly:

    for(int i = 2; i < array.length - 2; i++)
    {
        if(array[i] > (newArray[i] + 0.999))
        {
            count++;
            tempVal = count;
        }

        System.out.println(tempVal);
    }

If you want to save the two unused spaces, as you originally did, rather than responding exactly to the picture, then you should calculate the values as you originally did. But remember to compare each element to the one two places before it and stop 2 places before the end .

Instead of

for(int i = 2; i < array.length; i++)

use

for(int i = 2; i < array.length - 2; i++)

To print the location, your construct with the count and tempVal is unnecessary. You just need to print i+1 . Also note that you have a ; after your if . This means it's an empty if , and the block after it is always performed. Never have a ; after an if , for , while etc.

Not clear with what you are asking for in your question but without questioning what's the logic, by just looking at your code:

  for(int i = 2; i < array.length; i++) { if(array[i] > (newArray[i-2] + 0.999)); { count++; tempVal = count; } System.out.println(tempVal); } } 

if you relocate the system.out line as follows, I think you will get what you expect as follows:

    for(int i = 2; i < array.length - 2; i++)
    {
        if(array[i] > (newArray[i-2] + 0.999));
        {
            System.out.println(tempVal);
            // count++;
            // tempVal = count;
        }


    }   
    }

PS: Please note that I have also changed the boundary for the loop to stop iteration on 13th member of the array, instead of 15.

Are you sure you're parsing the numbers correctly? See Java: Reading integers from a file into an array Why don't you print them out after parsing for verification?

btw, this will overflow the index of the 2nd array (since it is created using new double[array.length - 4]):

for(int i = 2; i < array.length; i++)

so does your code run?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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