欧美日韩不卡一区二区三区,www.蜜臀.com,高清国产一区二区三区四区五区,欧美日韩三级视频,欧美性综合,精品国产91久久久久久,99a精品视频在线观看

java語(yǔ)言

java數(shù)組增刪改查操作方法

時(shí)間:2025-02-10 16:32:23 java語(yǔ)言 我要投稿
  • 相關(guān)推薦

java數(shù)組增刪改查操作方法

  數(shù)組常見(jiàn)的操作有遍歷,追加,插入元素,修改元素的值,刪除元素,反轉(zhuǎn),排序這些操作,本文演示遍歷,追加,插入元素,刪除元素,查詢(xún)這五個(gè)操作。那么java數(shù)組增刪改查操作方法是怎樣的?以下僅供參考!

  具體方法如下:

  數(shù)組是元素個(gè)數(shù)固定一種數(shù)據(jù)結(jié)構(gòu),這里所有說(shuō)的插入操作是邏輯上假定的一個(gè)新數(shù)組創(chuàng)建好后其有意義的邏輯元素為0個(gè)。

  public class Demo1 {

  public static void main(String[] args) {

  Scannerinput=new Scanner(System.in);

  int[] arr=newint[10];

  intindex=0;

  //追加元素

  for(inti=0;i<5;i++){

  System.out.println("請(qǐng)輸入一個(gè)整數(shù):");

  arr[index++]=input.nextInt();

  }

  for(inti=0;i<index;i++){

  System.out.print(arr[i]+" ");

  }

  System.out.println();

  //插入向數(shù)組下標(biāo)為1挺入新元素

  intpos;

  System.out.println("請(qǐng)輸入要插入的位置:");

  intIndex=input.nextInt();

  for( pos=index;pos>Index;pos-- ){

  arr[pos]=arr[pos-1];

  }

  System.out.println("請(qǐng)輸入要插入的值:");

  arr[pos]=input.nextInt();

  index++;

  for(inti=0;i<index;i++){

  System.out.print(arr[i]+" ");

  }

  System.out.println();

  System.out.println("請(qǐng)輸入要?jiǎng)h除元素的位置:");

  Index=input.nextInt();

  for( pos=Index;pos<index;pos++ ){

  arr[pos]=arr[pos+1];

  }

  index--;

  for(inti=0;i<index;i++){

  System.out.print(arr[i]+" ");

  }

  System.out.println();

  System.out.println("請(qǐng)輸入要查詢(xún)?cè)氐闹担?quot;);

  intvalue=input.nextInt();

  pos=-1;

  for(inti=0;i<index;i++){

  if(value==arr[i]){

  pos=i;break;

  }

  }

  if(pos!=-1){

  System.out.println("元素值為:"+value+"在數(shù)組中的位置是:"+(pos+1));

  }else{

  System.out.println("元素值為:"+value+"在數(shù)組不存在");

  }

  input.close();

  }

  }

【java數(shù)組增刪改查操作方法】相關(guān)文章:

Java數(shù)組的基本操作方法介紹08-14

php數(shù)組操作方法10-18

Java數(shù)組的使用07-14

Java數(shù)組操作的方法11-09

Java中數(shù)組的特性09-18

JAVA數(shù)組知識(shí)解析10-12

Java數(shù)組的基礎(chǔ)學(xué)習(xí)教程08-12

Java如何打印數(shù)組11-01

如何正確使用Java數(shù)組11-04