主要目的:
解决ArrayList 类不能改变大小的问题,主要实现数组列表动态调整大小。
1、数组类型如何选择?由于我们不清楚数组中具体存入什么类型的数据, 我们可以声明一个对象Object [ ] ,这样,数组列表就可以存储任何类型的数据了。
2、泛型<> :如果定义的一个类或接口有一个或多个类型变量,则可以使用泛型。
ArrayList<String>本身就是泛型,各种类型的变量都可以组装成对应的List,而不必针对每个类型分别实现一个构建ArrayList的类。
泛型字母所代表含义:
E表示集合的元素类型,
K 和 V分别表示表的关键字与值的类型 *
T(需要时还可以用临近的字母 U 和 S)表示“任意类型”
3、实现功能:我们主要实现arraylist的基本的增,删,改,等功能。
核心思路:主要根据所需求大小进行调整,需要创建一个新的数组,将老数组值赋予新数组再进行详细的变动。
?
|
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
package com.customArray0905;
public class CustomArraryList<E> {
Object[] data;
int Size;
public int getSize() {
return Size;
}
//返回数组下标为index的元素的值
public E get(int index) {
if(index<0 || index>=Size) {
throw new IndexOutOfBoundsException();
//否则return null
}
return (E) data[index];
}
//自定义更改下标为index的元素值的方法
public void set(int index, E e) {
if(index<0 || index>=Size) {
throw new IndexOutOfBoundsException();
//否则return null
}
data[index] = e;
}
public void add(E e) {
///创建新对象 容量扩大一个
Object[] newdata = new Object[Size + 1];
//将array中的元素重新存入更新容量后的newArray数组中去
for (int i = 0; i < Size; i++) {
newdata[i] = data[i];
}
data = newdata;
data[Size++] = e;
}
//自定义移除下标为index的元素的方法
public void remove(int index) {
///创建新对象 容量减少一个
Object[] newdata = new Object[Size - 1];
int j = 0;
//判断index大小是否合适存在数组中
if(index<0 || index>=Size) {
throw new IndexOutOfBoundsException();
//否则return null
}
//得到老对象里下标之前的所有元素并存入新对象
for (int i = 0; i < index; i++) {
newdata[j] = data[i];
j++;
}
//得到老对象里下标之后的所有元素并存入新对象
for (int i = index + 1; i < Size; i++) {
newdata[j] = data[i];
j++;
}
data = newdata;
Size--;
}
//清除array中所有的元素
public void clear() {
for(int i = 0;i<Size;i++) {
data[i] = null;
}
Size = 0;
}
public static void main(String[] args) {
CustomArraryList<String> myList = new CustomArraryList<>();
//Add
System.out.println("测试1,ADD方法");
myList.add("1");
myList.add("2");
myList.add("3");
myList.add("4");
myList.add("5");
for (int i = 0; i < myList.getSize(); i++) {
System.out.println(myList.get(i));
}
//Remove,Set
myList.remove(2);
myList.set(3, "7");
System.out.println("测试2,移除index=2的数据,并设置index=3的数据值为7,");
for (int i = 0; i < myList.getSize(); i++) {
System.out.println(myList.get(i));
}
//Clear
myList.clear();
myList.add("1");
for (int i = 0; i < myList.getSize(); i++) {
System.out.println("测试3,clear方法,仅剩下新添加数据 "+myList.get(i));
}
//抛出错误
System.out.println("测试4,抛出set错误");
myList.set(2,"2");
}
}
|
测试结果:
1、Arrays.toString
2、Arrays.deepToString 快速打印一个二维数组的数据元素列表
?
|
1
2
3
4
5
6
7
8
9
10
11
12
|
public static strictfp void main(String[] args) {
String[][] arr = {{"aaa","bbb"},{"ccc"}};
for(int x=0;x<arr.length;x++){
for(int y=0;y<arr[x].length;y++){
System.out.println(arr[x][y]);
}
}
//Arrays.deepToString 快速打印一个二维数组的数据元素列表
System.out.println(Arrays.deepToString(arr));
}
|
以上这篇Java自定义数组列表的实现操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。
原文链接:https://blog.csdn.net/weixin_49857492/article/details/108513330
相关文章
猜你喜欢
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 83
-
2025-05-25 66
-
2025-05-26 79
-
关于安装linux redhat后无法使用yum命令安装gcc-c++问题的解决过程
2025-05-27 54 -
2025-05-29 88
热门评论


