「java添加删除元素」java中怎么删除数组中的元素

博主:adminadmin 2023-01-10 19:48:09 1091

本篇文章给大家谈谈java添加删除元素,以及java中怎么删除数组中的元素对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java数组如何进行插入删除(很菜的问题)

java数组插入实现思路:

可以把某个整数插入到整型数组的特定位置,数组中原有元素向后移动

删除方法:能够从整形数组中特定位置删除掉一个元素,数组中原有元素向前移动

程序代码:

InsertDelete.java:

package p1;

import com.kettas.common.*;

import java.util.*;

public class InsertDelete

{

 static int[] a={4,2,7,3,5};//初始数组

 static int sum=5;//数组的有效位数

 public static void main(String[] args)

 {  

  System.out.println("初始数组为:");

  output();

  while(true)

  {

    System.out.println("操作:1:插入;2:删除;3:退出");

    int option=SystemIn.readInt();

    if(option==1)

    {

       System.out.println("请输入插入的位置:");

       int pos=SystemIn.readInt();

       System.out.println("请输入要插入的数:");

       int num=SystemIn.readInt();

       add(pos,num);

       System.out.println("插入后的数组为:");

       output();

    }

    else if(option==2)

    {

       System.out.println("请输入要删除的数的位置:");

       int pos1=SystemIn.readInt();

       move(pos1);

       System.out.println("删除后的数组为:");

       output();

    }

    else 

       break;

   }

  }

static void add(int pos,int num)

 {  //将num插入到数组的第pos下标中,其他元素向后移动

  if(sum == a.length)

  {     // 扩充数组空间

   a=Arrays.copyOf(a,a.length*2); 

  }

  for(int i=sum;ipos;i--)

  {

   a[i]=a[i-1]; 

  }

  a[pos]=num;

  sum++;  //有效位数加1

 }

 

 static void move(int pos)

 {  //删除数组中下标为pos的元素

  sum--;  //有效位数减1

  for(int i=pos;isum;i++) 

  {

   a[i]=a[i+1];

  }  

 }

 

 static void output()

 {   // 遍历输出数组中的元素

  for(int i=0;isum;i++)

  {

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

  }

  System.out.println();

 }

}

测试结果:

用java实现单链表元素的添加与删除

public class Link {

Node head = null;

Node point = null;

Node newNode = null;

public int Count = 0;//统计值

//插入

public void AddNode(int t) {

newNode = new Node();

if (head == null) {

head = newNode;

} else {

point = head;

while (point.next != null) {

point = point.next;

}

point.next = newNode;

}

point = newNode;

point.vlaue = t;

point.next = null;

Count++;

}

//返回值

public int GetValue(int i) {

if (head == null || i 0 || i Count)

return -999999;

int n;

Node temp = null;

point = head;

for (n = 0; n = i; n++) {

temp = point;

point = point.next;

}

return temp.vlaue;

}

//删除

public void DeleteNode(int i) {

if (i 0 || i Count) {

return;

}

if (i == 0) {

head = head.next;

} else {

int n = 0;

point = head;

Node temp = point;

for (n = 0; n i; n++) {

temp = point;

point = point.next;

}

temp.next = point.next;

}

Count--;

}

//排序

public void Sotr() {

for (Node i = head; i != null; i = i.next) {

for (Node j = i.next; j != null; j = j.next) {

if (i.vlaue j.vlaue) {

int t = i.vlaue;

i.vlaue = j.vlaue;

j.vlaue = t;

}

}

}

}

}

class Node {

int vlaue;

Node next;

}

如何改变java中的数组长度来增加或删除元素?

用顺序表java.util.List接口

你可以选择用ArrayList或是LinkedList,前者是数组实现,后者是链表实现。

例如: List list = new ArrayList();

具体的相关操作可以参考java提供的API文档

关于java添加删除元素和java中怎么删除数组中的元素的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。