当前位置:   article > 正文

向数组中添加元素_往数组里面添加元素

往数组里面添加元素

向数组中添加元素

1、数组末尾添加元素

(1)向数组末尾添加元素模型图

(2)向数组末尾添加元素代码

public class Array {
   
// 定义数组变量,data.length表示数组容量capacity
   
private int[] data;
   
// 定义数组中存放数据大小
   
private int size;

   
// 有参构造方法,传入数组的容量capacity构造动态数组
   
public Array(int capacity) {
       
data = new int[capacity];
       
size = 0;
   
}

   
// 无参构造方法,默认初始容量为capacity=10
   
public Array(){
       
this(10);
   
}

   
// 获取数组中元素个数
   
public int getSize(){
       
return size;
   
}

   
// 获取数组的容量
   
public int getCapacity(){
       
return data.length;
   
}

   
// 判断数组是否为空
   
public boolean isEmpty(){
       
return size == 0;
   
}

   
// 在数组末尾添加元素
    
public void addLast(int e){
       
if(size == data.length)
           
throw new IllegalArgumentException("AddLast failed.Array is full.");
       
data[size] = e;
       
size ++;
   
}
}

2、数组指定位置添加元素

(1)向数组指定位置添加元素模型图

(2)向数组指定位置添加元素代码

  1. public class Array {
  2.     // 定义数组变量,data.length表示数组容量capacity
  3.     private int[] data;
  4.     // 定义数组中存放数据大小
  5.     private int size;
  6.     // 有参构造方法,传入数组的容量capacity构造动态数组
  7.     public Array(int capacity) {
  8.         data = new int[capacity];
  9.         size = 0;
  10.     }
  11.     // 无参构造方法,默认初始容量为capacity=10
  12.     public Array() {
  13.         this(10);
  14.     }
  15.     // 获取数组中元素个数
  16.     public int getSize() {
  17.         return size;
  18.     }
  19.     // 获取数组的容量
  20.     public int getCapacity() {
  21.         return data.length;
  22.     }
  23.     // 判断数组是否为空
  24.     public boolean isEmpty() {
  25.         return size == 0;
  26.     }
  27.     // 在数组末尾添加元素
  28.     public void addLast(int e) {
  29.         if (size == data.length)
  30.             throw new IllegalArgumentException("AddLast failed.Array is full.");
  31.         data[size] = e;
  32.         size++;
  33.     }
  34.     // 数组指定位置添加元素
  35.     public void add(int index, int e) {
  36.         if (size == data.length)
  37.             throw new IllegalArgumentException("Add failed.Array is full.");
  38.         if (index < 0 || index > size)
  39.             throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size");
  40.         for (int i = size - 1; i >= index; i--)
  41.             data[i + 1] = data[i];
  42.         data[index] = e;
  43.         size++;
  44.     }
  45. }

(3)向数组末尾指定位置添加元素代码改造

  1. public class Array {
  2.     // 定义数组变量,data.length表示数组容量capacity
  3.     private int[] data;
  4.     // 定义数组中存放数据大小
  5.     private int size;
  6.     // 有参构造方法,传入数组的容量capacity构造动态数组
  7.     public Array(int capacity) {
  8.         data = new int[capacity];
  9.         size = 0;
  10.     }
  11.     // 无参构造方法,默认初始容量为capacity=10
  12.     public Array() {
  13.         this(10);
  14.     }
  15.     // 获取数组中元素个数
  16.     public int getSize() {
  17.         return size;
  18.     }
  19.     // 获取数组的容量
  20.     public int getCapacity() {
  21.         return data.length;
  22.     }
  23.     // 判断数组是否为空
  24.     public boolean isEmpty() {
  25.         return size == 0;
  26.     }
  27. /*    // 在数组末尾添加元素
  28.     public void addLast(int e) {
  29.         if (size == data.length)
  30.             throw new IllegalArgumentException("AddLast failed.Array is full.");
  31.         data[size] = e;
  32.         size++;
  33.     }*/
  34.     // 在数组末尾添加元素(复用add方法)
  35.     public void addLast(int e) {
  36.         add(size, e);
  37.     }
  38.     // 在数组头部添加元素(复用add方法)
  39.     public void addFirst(int e) {
  40.         add(0, e);
  41.     }
  42.     // 数组指定位置添加元素
  43.     public void add(int index, int e) {
  44.         if (size == data.length)
  45.             throw new IllegalArgumentException("Add failed.Array is full.");
  46.         if (index < 0 || index > size)
  47.             throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size");
  48.         for (int i = size - 1; i >= index; i--)
  49.             data[i + 1] = data[i];
  50.         data[index] = e;
  51.         size++;
  52.     }
  53. }

 

如果感兴趣的童鞋,可以观看我下一篇博客:数组中查询元素和修改元素

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/451104
推荐阅读
相关标签
  

闽ICP备14008679号