数据结构 Java数据结构 --- List ArrayList
有三种遍历方式:for循环,foreach,迭代器
public static void main(String[] args) {
ArrayList arrayList = new ArrayList<>();
arrayList.add(“hello”);
arrayList.add(“+”);
arrayList.add(“world!”);
System.out.println(arrayList);
System.out.println(“1.for循环”);
for (int i = 0; i < arrayList.size(); i++) {
System.out.print(arrayList.get(i)+" ");
}
System.out.println();
System.out.println(“2.foreach”);
for (String s:arrayList) {
System.out.print(s+" ");
}
System.out.println();
System.out.println(“3.使用迭代器的方法”);
Iterator it = arrayList.iterator();
while(it.hasNext()){
System.out.print(it.next()+" ");
}
System.out.println();
System.out.println(“4.迭代器List相关打印”);
ListIterator it2 = arrayList.listIterator();
while (it2.hasNext()){
System.out.print(it2.next()+" ");
}
4.2.1 迭代器 Iterator ListIterator


| 方法 | 解释 |
| :-- | :-- |
| boolean add(E e) | 尾插 e |
| void add(int index, E element) | 将 e 插入到 index 位置 |
| boolean addAll(Collection<? extends E> c) | 尾插 c 中的元素 |
| E remove(int index) | 删除 index 位置元素 |
| boolean remove(Object o) | 删除遇到的第一个 o |
| E get(int index) | 获取下标 index 位置元素 |
| E set(int index, E element) | 将下标 index 位置元素设置为 element |
| void clear() | 清空 |
| boolean contains(Object o) | 判断 o 是否在线性表中 |
| int indexOf(Object o) | 返回第一个 o 所在下标 |
| int lastIndexOf(Object o) | 返回最后一个 o 的下标 |
| List subList(int fromIndex, int toIndex) | 截取部分 list |
public static void main(String[] args) {
ArrayList arrayList = new ArrayList<>();
System.out.println(“==尾插e=”);
arrayList.add(“a”);
arrayList.add(“b”);
arrayList.add(“c”);
System.out.println(arrayList);
System.out.println(“==将 e 插入到 index 位置=”);
arrayList.add(0,“qwe”);
System.out.println(arrayList);
System.out.println(“==尾插 c 中的元素=”);
ArrayList arrayList1 = new ArrayList<>();
arrayList1.add(“!!!”);
arrayList.addAll(arrayList1);
System.out.println(arrayList);
System.out.println(“==删除 index 位置元素=”);
System.out.println(arrayList.remove(0));
System.out.println(arrayList);
System.out.println(“==删除遇到的第一个 o=”);
System.out.println(arrayList.remove(“a”));
System.out.println(arrayList);
System.out.println(“==获取下标 index 位置元素=”);
System.out.println(arrayList.get(1));
System.out.println(“==将下标 index 位置元素设置为 element=”);
System.out.println(“原来要修改的数据:”+arrayList.set(1,“d”));
System.out.println(arrayList);
System.out.println(“==判断 o 是否在线性表中=”);
System.out.println(arrayList.contains(“!!!”));
System.out.println(“==返回第一个 o 所在下标=”);
System.out.println(arrayList.indexOf(“!!!”));
System.out.println(“==返回最后一个 o 的下标=”);
System.out.println(arrayList.lastIndexOf(“!!!”));
System.out.println(“==截取部分 list=”);
List sub = arrayList.subList(0,2);//左闭右开
System.out.println(sub);
System.out.println(“==情况=”);
arrayList.clear();
System.out.println(arrayList);
}
运行结果:



**结论:
如果ArrayList调用无参的构造方法new ArrayList() , 那么顺序表的大小是0.当第一次add的时候, 整个顺序表才变为了10;
当这10个放满之后,才开始扩容,以1.5倍的方式扩容.
如果调用的是给定容量的构造方法new ArrayList(13) , 那么顺序表的大小就是给定容量的大小,如果放满了,还是以1.5倍进行扩容.**
=====================================================================================
import java.util.ArrayList;
class Student{
private String name;
private String classes;
private double score;
public Student(String name, String classes, double score) {
this.name = name;
this.classes = classes;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return “Student{” +
“name='” + name + ‘’’ +
“, classes='” + classes + ‘’’ +
“, score=” + score +
‘}’;
}
}
public class TestDemo10 {
public static void main1(String[] args) {
ArrayList students = new ArrayList<>();
students.add(new Student(“niubi”,“102-1”,98.9));
students.add(new Student(“21e”,“123”,22.2));
students.add(new Student(“qwq”,“wqqe”,455.4));
System.out.println(students);
}
}

运行结果:

public static void main(String[] args) {
ArrayList integers = new ArrayList<>();
integers.add(11);
integers.add(2);
integers.add(13);
Collections.sort(integers);
System.out.println(integers);
}
运行结果:

public static void main(String[] args) {
String str1 = “welcome to bit”;
String str2 = “come”;
ArrayList list1 = new ArrayList<>();
for (int i = 0; i < str1.length(); i++) {
char ch = str1.charAt(i);
if(!str2.contains(ch+“”)){
list1.add(ch);
}
}
for (char ch : list1 ) {
System.out.print(ch);
}
}
运行结果:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
class Card{
private int rank;
private String suit;
public Card(int rank, String suit) {
this.rank = rank;
this.suit = suit;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
@Override
public String toString() {
return “{” + suit + “->” +rank + “}”;
}
}
public class Test {
private static final String[] suits = {“❤”,“♠”,“♣”,“♦”};
/**
-
构造一副牌
-
@return
*/
public static List buyCard(){
ArrayList cards = new ArrayList<>();
//四种花色
for (int i = 0; i < 4; i++) {
//每个花色13张牌
for (int j = 0; j < 13; j++) {
/*String suit = suits[i];//花色
int rank = j ;//牌数
Card card = new Card(rank,suit);//得到一张牌card
cards.add(card);*/
cards.add(new Card(j,suits[i]));//优化后的代码
}
}
return cards;
}
private static void swap(List cards,int i,int j){
Card tmp = cards.get(i);
cards.set(i,cards.get(j));
cards.set(j,tmp);
}
/**
-
洗牌
-
@param cards
*/
public static void shuffle(List cards){
int size = cards.size();
for (int i = size - 1; i > 0; i–) {
Random random = new Random();
int rand = random.nextInt(i);
swap(cards,i,rand);
}
}
public static void main(String[] args) {
List cards = buyCard();
System.out.println(“刚开始的牌:”+cards);
shuffle(cards);
System.out.println(“洗过后的牌:”+cards);
ArrayList<List> hand = new ArrayList<>();
List hand1 = new ArrayList<>();
List hand2 = new ArrayList<>();
List hand3 = new ArrayList<>();
hand.add(hand1);
hand.add(hand2);
hand.add(hand3);
//每个人,轮流揭牌
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
Card card = cards.remove(0);//拿一张牌 少一张牌 remov去掉
hand.get(j).add(card);
}
}
System.out.println("第1个人的牌: "+hand1);
System.out.println("第2个人的牌: "+hand2);
System.out.println("第3个人的牌: "+hand3);
System.out.println("剩余的排: "+cards);
}
}
=====================================================================================
package demo;
import java.util.Arrays;
class MyArrayList {
private Object[] elem;//数组
private int usedSize;//代表有效数据个数
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
public MyArrayList() {
this.elem = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
public MyArrayList(int capacity) {
//对参数进行一个判断
if (capacity > 0) {
this.elem = new Object[capacity];
} else if (capacity == 0) {
this.elem = new Object[0];
} else {
throw new IllegalArgumentException(“不能为负数!”);
}
}
/**
-
尾插 e
-
@param e
-
@return
*/
public boolean add(E e) {
//缺点一个真正的容量.扩容
ensureCapacityInternal(usedSize + 1);
elem[usedSize] = e;
usedSize++;
return true;
}
private void ensureCapacityInternal(int minCapacity) {
//计算出需要的容量
int capacity = calculateCapacity(elem, minCapacity);
//拿着计算出的容量,去判断是否扩容,需要扩容就扩容
ensureExplicitCapacity(capacity);
}
private static int calculateCapacity(Object[] elem, int minCapacity) {
//1.是否elem数组分配过大小
if (elem == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(10, minCapacity);
}
//2.分配过 就返回+1的值
return minCapacity;
}
private void ensureExplicitCapacity(int minCapacity) {
if (minCapacity - elem.length > 0) {
//扩容
grow(minCapacity);
}
}
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
private void grow(int minCapacity) {
int oldCapacity = elem.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);//1.5倍扩容
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
//说明需要的容量非常大
newCapacity = hugeCapacity(minCapacity);
elem = Arrays.copyOf(elem, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0)
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
/**
-
将 e 插入到 index 位置
-
@param index
-
@param e
*/
public void add(int index, E e) {
rangeCheckForAdd(index);
ensureCapacityInternal(usedSize + 1);
copy(index, e);
usedSize++;
}
private void copy(int index, E e) {
for (int i = usedSize - 1; i >= index; i–) {
elem[i + 1] = elem[i];
}
elem[index] = e;
}
/**
-
判断index位置是否合法
-
@param index
*/
public void rangeCheckForAdd(int index) {
if (index < 0 || index > size()) {
throw new IndexOutOfBoundsException(“index位置不合法无法插入”);
}
}
/**
-
获取顺序表的大小
-
@return
*/
public int size() {
return this.usedSize;
}
/**
-
获取o第一次出现的位置
-
@param o
-
@return
*/
public int indexOf(Object o) {
if (null == o) {
for (int i = 0; i < usedSize; i++) {
if (elem[i] == null) {
return i;
}
}
} else {
for (int i = 0; i < usedSize; i++) {
if (elem[i].equals(o)) {
return i;
}
}
}
return -1;
}
/**
-
删出第一次出现的e
-
@param o
-
@return
*/
public boolean remove(Object o) {
int index = indexOf(o);
if (index == -1) {
return false;
}
remove(index);
return true;
}
/**
-
删除index位置上元素
-
@param index
-
@return
*/
public E remove(int index) {
rangeCheckForAdd(index);
E e = (E) elem[index];
// 将index之后的元素统一往前搬移一个位置
for (int i = index; i < usedSize - 1; ++i) {
elem[i] = elem[i + 1];
}
elem[usedSize] = null;
usedSize–;
return e;
}
/**
-
获取index位置上的元素
-
@param index
-
@return
*/
public E get(int index) {
rangeCheckForAdd(index);
return (E) elem[index];
}
/**
-
将index位置上元素设置为e
-
@param index
-
@param e
-
@return
*/
public E set(int index, E e) {
rangeCheckForAdd(index);
elem[index] = e;
return e;
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。


既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
《MySql面试专题》


《MySql性能优化的21个最佳实践》




《MySQL高级知识笔记》










文中展示的资料包括:**《MySql思维导图》《MySql核心笔记》《MySql调优笔记》《MySql面试专题》《MySql性能优化的21个最佳实践》《MySq高级知识笔记》**如下图

关注我,点赞本文给更多有需要的人
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
取!!(备注Java获取)**
《MySql面试专题》
[外链图片转存中…(img-zLJdsVeB-1713711978235)]
[外链图片转存中…(img-rWhqnjc0-1713711978235)]
《MySql性能优化的21个最佳实践》
[外链图片转存中…(img-aWXeM4YD-1713711978235)]
[外链图片转存中…(img-ea5Ggtjr-1713711978235)]
[外链图片转存中…(img-vnxywKPG-1713711978236)]
[外链图片转存中…(img-kAUQNcPq-1713711978236)]
《MySQL高级知识笔记》
[外链图片转存中…(img-D6157UuZ-1713711978236)]
[外链图片转存中…(img-eeF4nnYJ-1713711978236)]
[外链图片转存中…(img-Z20eJwzU-1713711978236)]
[外链图片转存中…(img-so8LXL4b-1713711978236)]
[外链图片转存中…(img-QZ6vSItO-1713711978237)]
[外链图片转存中…(img-kOtV6mva-1713711978237)]
[外链图片转存中…(img-3ogzaOuy-1713711978237)]
[外链图片转存中…(img-HKF5Cot0-1713711978237)]
[外链图片转存中…(img-ntLF421F-1713711978238)]
[外链图片转存中…(img-WGKUYOO9-1713711978238)]
文中展示的资料包括:**《MySql思维导图》《MySql核心笔记》《MySql调优笔记》《MySql面试专题》《MySql性能优化的21个最佳实践》《MySq高级知识笔记》**如下图
[外链图片转存中…(img-PtdjqB7u-1713711978238)]
关注我,点赞本文给更多有需要的人
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
更多推荐
所有评论(0)