国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - JAVA中數組插入與刪除指定元素的實例代碼

JAVA中數組插入與刪除指定元素的實例代碼

2021-03-31 12:00tangyaya8 JAVA教程

下面小編就為大家分享一篇JAVA中數組插入與刪除指定元素的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

今天學了Java的數組,寫了數組的插入和刪除,本人小白,寫給不會的小白看,大神請忽略,有錯請大家指出來;

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
給數組指定位置數組的插入
*/
import java.util.*;
public class ArrayInsert{
  public static void main(String []args){
    System.out.println("請用鍵盤輸入5個數:");
    int [] array =new int[10];
    Scanner sc=new Scanner(System.in);
    //通過鍵盤向數組輸入數
    for(int i=0;i<array.length-5;i++){
      array[i]=sc.nextInt();
    }
    //遍歷數組
    System.out.print("原數組為:");
    for(int a:array){
      System.out.print(" "+a);
    }
    //向指定的位置插入數
    System.out.println("\n請輸入插入位置:有效位置為0-----"+(array.length-1));
    int index=sc.nextInt();
    System.out.println("\n請輸入插入的值-----");
    int num=sc.nextInt();
 
    //調用靜態函數index
    //遍歷插入后的數組
      System.out.println("插入元素之后的數組遍歷:");
      Insert(index,num,array);
       for(int i=0;i<array.length;i++){
      System.out.print(" "+array[i]);
    }
  }
  //向數組指定位置插入數據方法
  public static int[] Insert(int index,int num,int a[]){
      //如果有元素,在索引之后的元素向后移一位,
      for(int  a[i]=a[i-1];
      }
      a[index]=num;
 return a;  
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//刪除數組指定位置的數字。
import java.util.*;
public class ArrayDelete{
  public static void main(String args[]){
    System.out.println("請用鍵盤輸入5個數:");
    int [] array =new int[10];
    Scanner sc=new Scanner(System.in);
    //通過鍵盤向數組輸入數
    for(int i=0;i<array.length-5;i++){
      array[i]=sc.nextInt();
    }
    //遍歷數組
    System.out.print("原數組為:");
    for(int a:array){
      System.out.print(" "+a);
    }
    //刪除在指定位置的數字
    System.out.println("\n輸入你要刪除的位置: 范圍在0---"+(array.length-1));
    int index=sc.nextInt();
    delete(index,array);//調用delete方法
    //刪除之后的遍歷
    System.out.println("刪除之后的遍歷:");
    for(int i=0;i<array.length;i++){
      System.out.print(" "+array[i]);
    }
  }
  public static int[] delete(int index,int array[]){
    //根據刪除索引,把數組后面的向前移一位
    for(int i=index;i<array.length;i++){
      if(i!=array.length-1){
        array[i]=array[i+1];
      }else{//處理最后一位超出情況
      array[i]=array[i];
      }
    }
    return array;
  }
}

以上這篇JAVA中數組插入與刪除指定元素的實例代碼就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/tangyaya8/article/details/76595488

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩欧美一级电影 | 看欧美一级片 | 国产成人高清 | 欧美一级欧美三级在线观看 | 欧美日韩国产精品一区二区 | 欧美激情国产日韩精品一区18 | 日韩精品视频在线观看免费 | 精品www| 亚洲午夜精品久久久久久高潮 | 国产欧美一区二区三区在线看 | 夜色影院在线观看 | 亚洲一区二区三区高清 | 国产毛片毛片 | 成人a在线 | 在线国产视频 | 久久综合另类激情人妖 | 国产一区二区三区免费在线观看 | jizzjizzjizz亚洲女 | 国产高清在线精品一区二区三区 | av网站免费看 | 欧美影视一区二区三区 | 久久久精品网站 | 亚洲九九 | 综合色导航 | 欧美日韩免费视频 | 日韩免费av一区二区 | 四虎久久精品 | 日韩一区二区三区在线视频 | 亚洲精品视频一区 | 精品国产欧美一区二区三区成人 | 性做久久久久久 | 国产精品久久久久久模特 | 中文字幕日韩欧美 | 欧美一区二区三区在线看 | 久久精品亚洲精品国产欧美kt∨ | 久久手机免费视频 | 9191国产视频| 黄工厂精品免费观看 | www.成人.com | 欧美视频在线观看 | 久久久久久久久久久福利观看 |