简体   繁体   中英

Java How to split array into two different arrays, and then add a number to the arrays

I have an unknown sized array, I only know that I only have odd sized numbers.

I need to be able to check if the first half of the array grows progressively towards the center value (the maximum value), while after the center value the values of the numbers progressively becomes lower.

I thought the best way would be to split the length of the array, but because it is an odd number I need to add 0.5 to the array, which doesn't seem to be possible.

int N = Integer.parseInt(args[0]);

int[] array = new int[N];

int x = array.length;

//split into new arrays
int[] a = new int [x/2];
int[] b = new int [x/2];

if(a<b && b<c){
    System.out.println("Has a peak");
} else {
    System.out.println("Doesnt have a peak"); 
}

The center value doesn't need to be in one of the child arrays. If the original is N=11, then you could split things as 5-1-5. But as Bob Brinks' comment says, there's no need to actually create separate arrays, you just need to use ints for the appropriate indexes, and loops to go over the array itself and check that each cell follows the rule. Set a bool called "ok" to true before the loops, and set it to false if any array index fails the test.

The center cell will always be at array[N/2] (N/2 is always rounded down). you can check each cell from array[1] to array[N/2] and make sure each one is bigger than the one before. you then have a second loop, which looks at each cell from array[N/2+1] to array[N-1] and check that each of those is less than the one before. Then after the loops, you see if "ok" is still true, in which case none of the cells broke the rule.

The main thing to look out for is to print the cell index each time through the loop and make sure that they're all being triggered in the correct order (start and end points), if not, then you had a problem in how you wrote your FOR loops, the other thing is whether two equal values in a row are allowed. Use >= and <= for the comparison checks if that's the case. Printing out values as you go is really your friend here: in debug versions of programs, print out lengths of arrays, loop indexes etc, basically any values you can identify. It makes noticing "hey there's a problem here" and learning how all the program structures work much much easier.

try this function. It takes an array as a parameter and returns true if the array contains a peak(according to your explanation) and false if there are none -

public static boolean isPeak(int[] array){
    int length = array.length;
    int prev=array[0];
    for(int index=1;index<length;index++){
        if(index<length/2+1){
            if(array[index] < prev){
                return false;
            } else {prev=array[index];}
        } else {
            if(array[index] > prev){
                return false;
            } else {prev=array[index];}
        }
    }
    return true;
}

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