1. java數組元素刪除
編程語言的數組中刪除一個元素的方法是各種語言都一樣的。比如說再有十個元素的輸出中要刪除第三個元素,就從第三個元素一直到第九個元素,使用循環,每次把後一個元素往前復制一位,最後把數組的元素個數減一。
2. 在JAVA中如何從數組中刪除一個元素
1、打開myeclipse的主頁以後,直接根據實際情況新建一個相關文件。
3. java如何刪除掉數組中的某個元素
String[]
arrays={"1","2","3","5","6"};
String[]
tempArr
=
new
String[arrays.length];
int
i
=
0;
for(String
s:arrays){
if(!s.equals("2")){
tempArr[i]
=
s;
i++;
}
}
for(int
j
=
0;
j
<
tempArr.length;
j++)
{
System.out.println(tempArr[j]);
}
//
數組本身是不可以移除元素的
但可以通過中間變數來實現數組的移除
4. java中怎麼樣刪除數組中的指定元素
public static void main(String[] args) {
String[] arrays={"1","2","3","5","6"};
for (int i = 0; i < arrays.length; i++) {
if("2".equals(arrays[i])){
//移除掉元素2
for (int j = i+1; j < arrays.length; j++) {
arrays[i]=arrays[j];
}
}
}
}
5. java怎麼 清除數組數據
有兩種方法:
1.
使用循環,在循環裡面調用remove(下標)來循環刪除數組中的每一個數據
2.
將你的數組重新new一下,這樣就將原來的數組覆蓋清空了
6. java怎麼刪數組里的數據
數組刪除數據不是很方便的,因為中間空了,需要把刪除的index的後面的元素依次往前移動
如果不是一定要用數組,可以用java提供的 ArrayList 和 LinkedList,都有提供刪除元素的操作,不過後者底層是鏈表實現,刪除的效率很高, O(1) 的操作;ArrayList 效率低一些
7. java如何刪除數組的元素
樓主你好
具體代碼如下:
public class Test {
private int a[] = {1,2,3,4,5};//數組初始值1 2 3 4 5
public void delete(int n)//刪除數組中n的值
{
for (int i = 0; i < a.length; i++) {
if(a[i] == n)
{
for(int j = i; j < a.length-1; j++)
{
a[j] = a[j+1];
}
}
}
}
public void print()//列印數組
{
for (int i = 0; i < a.length-1; i++) {
System.out.println (a[i]);
}
}
public static void main(String[] args) {
Test t = new Test();
t.delete(4);
t.print();
}
}
運行結果:
1
2
3
5
希望能幫助你哈