在数组中的任何位置插入元素

发布于 2025-01-20 12:29:31 字数 698 浏览 0 评论 0原文

我对编程的新手相对较新,对一些帮助将不胜感激。我正在收到错误消息:数组索引从界限出来。

static int[] insert1(int z, int arr[], int ins, int p)
{
    int i;


    int newarray[] = new int [z + 1];

    for (i = 0; i < z + 1 ; i++) {
        if (i < p - 1 )
            newarray[i] = arr[i];
        else if (i == p - 1)
            newarray[i] = ins;
        else
            newarray[i] = arr[i - 1];
    }
    return newarray;

}
 public static void main(String[] args) {

    int ins = 20;
    int z = 5;

    Scanner scan = new Scanner(System.in);
    int p = Integer.parseInt((scan.next()));

    arr = insert1(a, arr, ins, p);

    System.out.println("Insert Array:\t" + Arrays.toString(arr));

 }
}

I'm relatively new to programming and some help would be appreciated. I'm receiving the error message: array index out of bounds.

static int[] insert1(int z, int arr[], int ins, int p)
{
    int i;


    int newarray[] = new int [z + 1];

    for (i = 0; i < z + 1 ; i++) {
        if (i < p - 1 )
            newarray[i] = arr[i];
        else if (i == p - 1)
            newarray[i] = ins;
        else
            newarray[i] = arr[i - 1];
    }
    return newarray;

}
 public static void main(String[] args) {

    int ins = 20;
    int z = 5;

    Scanner scan = new Scanner(System.in);
    int p = Integer.parseInt((scan.next()));

    arr = insert1(a, arr, ins, p);

    System.out.println("Insert Array:\t" + Arrays.toString(arr));

 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹤仙姿 2025-01-27 12:29:31

对于初学者来说,不必将长度作为输入,这将使您的生活变得更加轻松。只需使用 arr.length 就会得到相同的结果,而不必担心另一个变量。

您的插入有一个良好的开始。一种更有效、更简单的方法是使用多个 for 循环。

int newArray[] = new int[arr.length + 1];
for (int i = 0; i < p; i++) {
    newArray[i] = arr[i];
}
newArray[p] = ins;
for (int i = p + 1; i < newArray.length; i++) {
    newArray[i] = arr[i - 1];
}
return newArray;

如果这不能满足您的要求,请告诉我,我可以对其进行修改。

PS 如果您愿意更改更多代码,您应该考虑使用 列表 代替。我很乐意在我的答案中添加一个带有列表的版本。

For starters, it will make your life a lot easier to not have to put the length in as an input. Just using arr.length will give you the same result without having to worry about another variable.

You have a good start for an insertion. A more efficient and less complex way to do it would be with multiple for-loop.

int newArray[] = new int[arr.length + 1];
for (int i = 0; i < p; i++) {
    newArray[i] = arr[i];
}
newArray[p] = ins;
for (int i = p + 1; i < newArray.length; i++) {
    newArray[i] = arr[i - 1];
}
return newArray;

Let me know if this doesn't do what you're looking for and I can modify it.

P.S. If you are comfortable with changing more of your code you should look into using Lists instead. I'd be happy to add a version with a list to my answer.

戈亓 2025-01-27 12:29:31

我将使用此方法来将操作插入数组中,以通过将元素向右移动到所需位置的右侧来执行的任何位置,例如:

{1,2,2,3,3,4,5 ,6、7、8、9 10},我们要插入x作为所需位置的元素x = 50;

to : {1 , 2 , 3 , 4 , 50 , 5 , 6 , 7 , 8 , 9 , 10}

特定pos = 5 ;

//this to insert element at a specific position 
//in an array

class Insertion {

//shift elements to the right side
//which are on the right side of pos 
  static void isertElement(int arr[] , int n , int x , int pos ){
        for(int i = n - 1; i > = pos; i--) {
                arr[i + 1] = arr[i];
                arr[i] = x; 
                       
            }       
                
    }
       

    public static void main(String args []) {
         int arr[] = new int[6];
         arr[0] = 2;
         arr[1] = 4;   
         arr[2] = 1;
         arr[3] = 8;
         arr[4] = 5;
         
         int n = 5;            
         int x = 10 , pos = 2; 

         System.out.print("Before the isertion");
           for(int i = 0; i < n; i++) {
               System.out.print(arr[i] + " ");  
          
           }
     
        //inserting key at specific position     
        insertElement(arr , n , x , pos);
        n += 1; 
       
        System.out.print("\n After the insertion: ");  
          for(int i = 0; i < n; i++){
             System.out.print(arr[i] + " ");

         }


    }
     


  }

I will do with this method to insert operation in an array at any position I had performed by shifting elements to the right , which are on the right side of required position for instance :

{1 , 2 , 3 , 4 ,5 ,6 ,7 ,8 ,9 10} and we want to insert x as element at required position x = 50;

to : {1 , 2 , 3 , 4 , 50 , 5 , 6 , 7 , 8 , 9 , 10}

specific pos = 5;

//this to insert element at a specific position 
//in an array

class Insertion {

//shift elements to the right side
//which are on the right side of pos 
  static void isertElement(int arr[] , int n , int x , int pos ){
        for(int i = n - 1; i > = pos; i--) {
                arr[i + 1] = arr[i];
                arr[i] = x; 
                       
            }       
                
    }
       

    public static void main(String args []) {
         int arr[] = new int[6];
         arr[0] = 2;
         arr[1] = 4;   
         arr[2] = 1;
         arr[3] = 8;
         arr[4] = 5;
         
         int n = 5;            
         int x = 10 , pos = 2; 

         System.out.print("Before the isertion");
           for(int i = 0; i < n; i++) {
               System.out.print(arr[i] + " ");  
          
           }
     
        //inserting key at specific position     
        insertElement(arr , n , x , pos);
        n += 1; 
       
        System.out.print("\n After the insertion: ");  
          for(int i = 0; i < n; i++){
             System.out.print(arr[i] + " ");

         }


    }
     


  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文