简体   繁体   English

如何在给定位置的数组中放置元素?

[英]How can i place an element in an array on a given position?

I have the following question. 我有以下问题。

int[] ar={4,6,7,8}

Now i want to add an element so i get 现在我想添加一个元素,这样我就可以

ar={4,6,9,7,8}

I want to be able to add a given element (9) in a position that i want i this case index 2. How? 我希望能够在此案例索引2的位置添加给定元素(9)。如何?

In Java, the size of an array cannot be changed. 在Java中,无法更改数组的大小。 Instead you can use a List : 相反,您可以使用List

List<Integer> numList = new ArrayList<Integer>();
numList.add(4);
numList.add(6);
numList.add(7);
numList.add(8);

Then you can use numList.add(int index, E element); 然后可以使用numList.add(int index, E element); to insert values at specific positions. 在特定位置插入值。

numList.add(2, 9);
//numList = {4, 6, 9, 7, 8};

For further information you may want to look at this tutorial . 有关更多信息,您可能需要阅读本教程

Java arrays are fixed length, so you'll need to create another one to store your extra element. Java数组是固定长度的,因此您需要创建另一个数组来存储您的额外元素。

int[] ar = { 4, 6, 7, 8 };
int[] tmp = new int[ar.length + 1];
int pos = 2;
for (int i = 0; i < pos; i++) {
    tmp[i] = ar[i];
}
for (int i = pos + 1; i <= ar.length; i++) {
    tmp[i] = ar[i - 1];
}
tmp[pos] = 9;
System.out.println(Arrays.toString(tmp));

Output is (as requested) 输出是(根据要求)

[4, 6, 9, 7, 8]

How to add new elements to an array? 如何将新元素添加到数组?

add an element to int [] array in java 在Java中将元素添加到int []数组中

Note that ArrayList also has an add method that lets you specify an index and the element to be added void add(int index, E element) . 请注意,ArrayList也有一个add方法,该方法可让您指定索引和要添加的元素void add(int index, E element)

The most simple way of doing this is to use an ArrayList<Integer> and use the add(int, T) method. 最简单的方法是使用ArrayList<Integer>并使用add(int, T)方法。

List<Integer> numList = new ArrayList<Integer>();
numList.add(4);
numList.add(6);

// Now, we will insert the number
numList.add(2, 9);

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

相关问题 如何将字符串拆分为数组,然后返回给定元素? - How can I split a String into an Array then return a given element? 如何显示数组中元素的位置? - How would I display the position of an element in an array? 在给定位置返回数组元素的方法? - Method to return an array element at a given position? 根据给定java的位置更改数组中的元素 - change element in array based on position given java 如何通过使用索引指定数组 position 来打印多维数组元素? - How can I print an multidimensional array element by specifying the array position with an index? 我如何放置JOptionPane。 showInputDialog到屏幕上的第一个位置? - How can i place JOptionPane . showInputDialog to the first position on the screen? 如何优化我的代码以交换给定范围的索引的数组元素与相关元素? - How can I optimize my code for Swapping the array elements of given range of indexes with related element? 如何根据以 dd/mm/yyyy 格式给出的日期数组 [0] 元素对数组的 ArrayList 进行排序? - How can I sort an ArrayList of arrays based on the array[0] element which is a date given in dd/mm/yyyy format? 如何获得一个数组,将零放在第一个元素上而不跳过它? - How do I get an array to place a zero on the first element and not skip it? 如何找到给定链表位置的节点? - How can I find the node given a position of a linkedlist?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM