简体   繁体   English

Java如何将数组拆分为两个不同的数组,然后在数组中添加一个数字

[英]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.我认为最好的方法是拆分数组的长度,但是因为它是一个奇数,所以我需要将 0.5 添加到数组中,这似乎是不可能的。

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.如果原始值是 N=11,那么您可以将事物拆分为 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.但正如 Bob Brinks 的评论所说,实际上没有必要创建单独的数组,您只需要使用整数作为适当的索引,并循环遍历数组本身并检查每个单元格是否遵循规则。 Set a bool called "ok" to true before the loops, and set it to false if any array index fails the test.在循环之前将名为“ok”的 bool 设置为 true,如果任何数组索引未通过测试,则将其设置为 false。

The center cell will always be at array[N/2] (N/2 is always rounded down).中心单元将始终位于数组 [N/2](N/2 始终向下舍入)。 you can check each cell from array[1] to array[N/2] and make sure each one is bigger than the one before.您可以检查从 array[1] 到 array[N/2] 的每个单元格,并确保每个单元格都比之前的大。 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.然后你有第二个循环,它查看从 array[N/2+1] 到 array[N-1] 的每个单元格,并检查每个单元格是否小于之前的单元格。 Then after the loops, you see if "ok" is still true, in which case none of the cells broke the rule.然后在循环之后,您会看到“ok”是否仍然为真,在这种情况下,没有一个单元格违反规则。

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.要注意的主要事情是每次通过循环打印单元格索引并确保它们都以正确的顺序(起点和终点)被触发,如果不是,那么你的方式有问题写了你的 FOR 循环,另一件事是是否允许连续两个相等的值。 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 -它接受一个数组作为参数,如果数组包含一个峰值(根据你的解释),则返回true如果没有,则返回false -

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;
}

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

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