将整数插入数组java

时间:2016-02-16 05:00:42

标签: java arrays

我想在一个正确位置的数组中插入一个整数。

例如,我的数组是(1,2,3,4,6),我的整数是5 新数组应为(1,2,3,4,5,6)

到目前为止我的代码:

public static void main(String[] args) {

    int a = 5;
    int [] aray =  { 1,2,3,4,6};
    int[] newArray = new int[array.length + 1];

    for(int i = 0; i < newArray.length -1 ; i++) {

       if(array[i] < a && array[i+1] > a) { 
            newArray[i]  = a; 
       }               
       newArray[i] = array[i];                                  
    }                    

}

6 个答案:

答案 0 :(得分:1)

下面的代码按预期工作。

int a = 4;
int[] array = { 1, 3, 5, 7, 9 };
int[] newArray = new int[array.length + 1];
for (int i = 0, j = 0; i < array.length; i++, j++) {
    if (i == 0 && array[0] > a){
        j++;
        newArray[j] = a;
    }
    newArray[j] = array[i];
    if((i == array.length - 1 && array[i]<a)
            || (array[i] < a && array[i + 1] > a)) {
        j++;
        newArray[j] = a;
    }

}
System.out.println(Arrays.toString(newArray));

答案 1 :(得分:0)

您可以使用两个循环实现此功能。一个用于迭代从原始复制到新复制,直到您将newArray添加到newArray,然后是第二个循环来复制从原始数组中留下的所有内容(进入if)。最后,您需要最后int i = 0, p = 0; while (p < newArray.length && i < array.length) { if (array[i] < a) { newArray[p++] = array[i++]; } else { newArray[p++] = a; break; // <-- added a, break and add the rest of the original array } } while (p < newArray.length && i < array.length) { newArray[p++] = array[i++]; } if (p < newArray.length) { newArray[p] = a; } System.out.println(Arrays.toString(newArray)); 来确保您的元素不应添加到最后。像,

build.gradle

答案 2 :(得分:0)

您可以尝试这样做:您可以使用System.arraycopy()代替for循环。

int a = 5;
int [] array =  { 1,2,3,4,6};
int[] newArray = new int[array.length + 1];
// 1. copy the array
System.arraycopy(array, 0, newArray, 0, array.length);
// 2. insert the value to the last position.
newArray[newArray.length-1] = a;
// 3. then sort the array
Arrays.sort(newArray);

// or you can swap the position for this
/*int temp = newArray[4];
newArray[4] = a;
newArray[5] = temp;*/

// 4. and it is what you want
System.out.println("newArray = " + Arrays.toString(newArray));

结果是:

newArray = [1, 2, 3, 4, 5, 6]

答案 3 :(得分:0)

for (int i = 0; i < newArray.length - 1; i++) {
    if (array[i] < a && array[i + 1] > a) {
        int t = array[i + 1];
        newArray[i + 1] = a;
        newArray[i + 2] = t;
        newArray[i] = array[i];
        break;//use break
    }
    newArray[i] = array[i];
}

答案 4 :(得分:0)

请检查以下代码

int a = 5;
int [] aray =  { 1,2,3,4,6};
int[] newArray = new int[aray.length + 1];
for(int i=0, j=0;  i<aray.length;   i++, j++) {
      if((aray[i] > a) && (j == i)) {
            newArray[j] = a;
            j++;
       }
       newArray[j] = aray[i];
}
if(j == i) {
    newArray[j] = a;
}

答案 5 :(得分:-1)

class test{
public static void main(String args[]){
        int arr[] ={1,2,3,4,5};
        int new_array[] =new int[arr.length+1];
        int e = 9;
        int p = 3;
        for(int i=0;i<=arr.length-1;i++){
                if(i==p){
                        new_array[i] = e;
                        continue;
                    }
                new_array[i]=arr[i];
            }
    }

}