本文整理了java数据结构的数组操作。希望对刚入门数据结构的同志们有帮助。
java数组非常简单。只要有JAVA语言基础就可以看这篇博文。大家不要害怕。。。非常简单。。
整理博客真的很花费时间,如果对大家有帮助,麻烦点赞评论,让我有动力继续更新下去,谢谢大家。
数组基础
数组的结构如下:
我们在存数据的时候,存在数组里面,以索引来读取数据。可以通过数组名【索引】的形式访问。每一个格子都存放一个数据。
创建一个索引的代码:
/**
* Created by hongyonghan
*/
public class Main {
public static void main(String[]args){
//方法一:声明一个数组,并给定数组长度
int [] arr=new int[10];
for (int i=0;i<arr.length;i++)
{
//给arr数组赋值赋值
arr[i]=i;
}
//方法二:直接给数组赋初值
int[] scores=new int[]{100,99,66};
for (int i=0;i<scores.length;i++)
{
System.out.println(scores[i]);
}
//使用foreach的形式遍历数组
for (int score:scores)
{
System.out.println(score);
}
//更改数组的值
scores[0]=98;
for (int score:scores)
{
System.out.println(score);
}
}
}
数组的优点和不足:
如果使用上面身份证号作为一个索引的话,就需要开辟这么大的数组空间,对计算机来说开辟这么大空间是不现实的。
java数组本身没有提供添加、删除数组元素这些功能,所以需要我们自己二次封装我们自己的数组。
上面的size是我们数组中有实际值元素的个数,而capacity是数组的容量。
下面是创建一个数组及初始化。
private Array(int capacity)
{
data=new int[capacity];
size=0;
}
//无参数的构造函数
public Array() {
//如果用户不传入capacity的话,就默认构建一个长度是10的数组。
this(10);
}
//获取数组中的元素个数
public int getSize()
{
return size;
}
//获取数组的容量
public int getCapapcity()
{
return data.length;
}
//获取数组是否为空
public boolean isEmpty()
{
return size==0;
}
向数组中添加元素
向数组末添加元素就是给定一个值,然后就可以直接添加进去。这里解释一下:这里的size值在最开始创建数组的时候,就是指在数组长度的位置上,也就是0那个位置。那么添加的话,也是添加在0那个位置。以后我们每添加一个元素除了给那个位置添加一个元素以外,还有给size的位置加一,目的是让这个size指在前面的位置上。
//向所有元素后添加一个新元素
public void addLast(int e)
{
if (size==data.length)
{throw new IllegalArgumentException("failed;array is full");}
//给size位置赋值。
data[size]=e;
//更新size的位置
size++;
}
向数组中指定位置添加元素
做法就是将指定位置以后的元素都往后挪动一个位置。下面是添加过程:
下面第一个图是一开始数据的位置,我们将77插入到索引位置为1的位置。那么将索引是1之后的所有元素后移,然后将77放在索引是1的位置的地方。
后移的过程是:将前面的值赋值给后面的值。比如这里将100赋值在size指向的位置,将99赋值到100位置处。
然后将元素放在这个位置,并且size也要加加。
//在第index个位置插入元素e
public void add(int index,int e)
{
if (size==data.length)
{throw new IllegalArgumentException("failed;array is full");}
if(index <0 || index > size )
{
throw new IllegalArgumentException("failed;index >= 0 and index <=size");
}
for (int i = size-1;i >= index; i--)
{
data[i+1]=data[i];
}
data[index]=e;
size++;
}
//在所有元素前添加一个新元素添加元素
public void addFirst(int e)
{
add(0,e);
}
//根据index索引位置获取元素
public int get(int index)
{
if (index <0 || index >= size)
{
throw new IllegalArgumentException("failes,index is illegal");
}
return data[index];
}
//修改index处位置为e
public void set(int index,int e)
{ if (index <0 || index >= size)
{
throw new IllegalArgumentException("failes,index is illegal");
}
data[index]=e;
}
//查看元素中是否包含元素e
public boolean contains(int e)
{
for (int i=0;i<size;i++)
{
if (data[i]==e)
{
return true;
}
}
return false;
}
//查找数组中元素e所在的索引,如果不存在元素e,则返回-1
public int find(int e)
{
for (int i=0;i<size;i++)
{
if (data[i]==e)
{
return i;
}
}
return -1;
}
从数组中删除元素
数组中删除元素就是将某个位置之后的全部值前移,覆盖要删除位置的元素,然后size减减。
size要进行减减。
//从数组中删除index位置的元素,返回删除的元素
public int remove(int index)
{
if (index <0 || index >= size)
{
throw new IllegalArgumentException("failes,index is illegal");
}
int ret=data[index];
for (int i=index+1;i<size;i++)
{
data[i-1]=data[i];
}
size--;
return ret;
}
//从数组中删除第一个元素,返回删除的元素
public int removeFirst()
{
return remove(0);
}
//从数组中删除最后一个元素,返回删除元素。
public int removeLast()
{
return remove(size-1);
}
//从数组中删除元素e
public boolean removeElement(int e)
{
int index=find(e);
if (index != -1)
{
remove(index);
return true;
}
return false;
}
泛型
public class Array <E>{}
加了声明成为一个泛型数组,那么这个E就是数组中存放元素的类型。可以是int,可以是string等等。
全部代码如下:
/**
* Created by hongyonghan on 2020/4/12.
*/
public class Array <E>{
//定义数组
private E[] data;
//定义数组中元素的个数
private int size;
//capacity相当于是设定数组的大小
public Array(int capacity)
{
data= (E[]) new Object[capacity];
size=0;
}
//无参数的构造函数
public Array() {
//如果用户不传入capacity的话,就默认构建一个长度是10的数组。
this(10);
}
//获取数组中的元素个数
public int getSize()
{
return size;
}
//获取数组的容量
public int getCapapcity()
{
return data.length;
}
//获取数组是否为空
public boolean isEmpty()
{
return size==0;
}
//向所有元素后添加一个新元素
// public void addLast(int e)
// {
// if (size==data.length)
// {throw new IllegalArgumentException("failed;array is full");}
// data[size]=e;
// size++;
// //复用add
size=getSize();
add(size,e);
// }
//在第index个位置插入元素e
public void add(int index,E e)
{
if(index <0 || index > size )
{
throw new IllegalArgumentException("failed;index >= 0 and index <=size");
}
if (size==data.length)
{
//动态数组
resize(2*data.length);
}
for (int i = size-1;i >= index; i--)
{
data[i+1]=data[i];
}
data[index]=e;
size++;
}
public void addLast(E e)
{
add(size,e);
}
//在所有元素前添加一个新元素添加元素
public void addFirst(E e)
{
add(0,e);
}
//根据index索引位置获取元素
public E get(int index)
{
if (index <0 || index >= size)
{
throw new IllegalArgumentException("failes,index is illegal");
}
return data[index];
}
//修改index处位置为e
public void set(int index,E e)
{ if (index <0 || index >= size)
{
throw new IllegalArgumentException("failes,index is illegal");
}
data[index]=e;
}
//查看元素中是否包含元素e
public boolean contains(E e)
{
for (int i=0;i<size;i++)
{
if (data[i].equals(e))
{
return true;
}
}
return false;
}
//查找数组中元素e所在的索引,如果不存在元素e,则返回-1
public int find(E e)
{
for (int i=0;i<size;i++)
{
if (data[i].equals(e))
{
return i;
}
}
return -1;
}
//从数组中删除index位置的元素,返回删除的元素
public E remove(int index)
{
if (index <0 || index >= size)
{
throw new IllegalArgumentException("failes,index is illegal");
}
E ret=data[index];
for (int i=index+1;i<size;i++)
{
data[i-1]=data[i];
}
size--;
data[size]=null;//这句话不是必须的,因为用户是无法访问到的。
//减小容量
if(size == data.length/4 && data.length /2 !=0 )
{
resize(data.length/2);
}
return ret;
}
//从数组中删除第一个元素,返回删除的元素
public E removeFirst()
{
return remove(0);
}
//从数组中删除最后一个元素,返回删除元素。
public E removeLast()
{
return remove(size-1);
}
//从数组中删除元素e
public boolean removeElement(E e)
{
int index=find(e);
if (index != -1)
{
remove(index);
return true;
}
return false;
}
//可以做findALL和removeAll函数
@Override
public String toString() {
StringBuilder res=new StringBuilder();
res.append(String.format("Array: size = %d,capacity = %d \n",size,data.length));
res.append("[");
for (int i=0;i < size;i++)
{
res.append(data[i]);
if (i != size-1)
{
res.append(", ");
}
}
res.append("]");
return res.toString();
}
泛型就是用来处理数组中元素类型不同的问题的,使用泛型可以让我们自定义数据类型。如下是自定义的数据类型对象:
/**
* Created by hongyonghan
*/
public class Student {
private String name;
private int score;
public Student() {
}
public Student(String name, int score) {
this.name = name;
this.score = score;
}
@Override
public String toString() {
return String.format("Student(name:%s, score: %d)",name,score);
}
public static void main(String[]args){
Array<Student> array=new Array<>();
array.addLast(new Student("Alice",100));
array.addLast(new Student("Bob",66));
array.addLast(new Student("Hahan",88));
System.out.println(array);
}
}
动态数组
数组原数据:
//在第index个位置插入元素e
public void add(int index,E e)
{
if(index <0 || index > size )
{
throw new IllegalArgumentException("failed;index >= 0 and index <=size");
}
if (size==data.length)
{
//动态数组添加
resize(2*data.length);
}
for (int i = size-1;i >= index; i--)
{
data[i+1]=data[i];
}
data[index]=e;
size++;
}
private void resize(int newCapacity)
{
//新建一个泛型数组,将之前的数组复制到新的数组当中。
E[] newData= (E[]) new Object[newCapacity];
for (int i=0;i<size;i++)
{
newData[i]=data[i];
}
data=newData;
}
}