什么是List
List是一個(gè)接口,繼承自Collection。
List的使用
List是個(gè)接口,并不能直接用來(lái)實(shí)例化。
如果要使用,必須去實(shí)例化List的實(shí)現(xiàn)類(lèi)。在集合框架中,ArrayList和LinkedList都實(shí)現(xiàn)了List接口。
線(xiàn)性表
線(xiàn)性表(linear list)是n個(gè)具有相同特性的數(shù)據(jù)元素的有限序列。
常見(jiàn)的線(xiàn)性表:順序表、鏈表、棧、隊(duì)列…
線(xiàn)性表在邏輯上是線(xiàn)性結(jié)構(gòu),也就說(shuō)是連續(xù)的一條直線(xiàn)
順序表
順序表是用一段物理地址連續(xù)的存儲(chǔ)單元依次存儲(chǔ)數(shù)據(jù)元素的線(xiàn)性結(jié)構(gòu),一般情況下采用數(shù)組存儲(chǔ)。在數(shù)組上完成
數(shù)據(jù)的增刪查改
接口的實(shí)現(xiàn)
類(lèi)的成員
打印順序表
對(duì)上述功能進(jìn)行測(cè)試
上述兩個(gè)功能的測(cè)試
所有代碼如下
PosOutBoundsException
public class PosOutBoundsException extends RuntimeException{
public PosOutBoundsException() {
}
public PosOutBoundsException(String message) {
super(message);
}
}
SeqList
public class SeqList {
private int[] elem;
private int usedSize;記錄當(dāng)前順序表當(dāng)中 有多少個(gè)有效的數(shù)據(jù)
private static final int DEFAULT_CAPACITY=2;
public SeqList() {
this.elem =new int[DEFAULT_CAPACITY];
}
//打印順序表
public void display(){
for (int i = 0; i <this.usedSize; i++) {
System.out.print(this.elem[i]+" ");
}
System.out.println();
}
//新增元素,默認(rèn)在所有數(shù)據(jù)的結(jié)尾處添加
public void add(int data){
if(isFull()){
resize();
System.out.println("擴(kuò)容成功,當(dāng)前容量為"+this.elem.length);
}
this.elem[usedSize]=data;
usedSize++;
System.out.println("尾部添加元素成功");
}
public boolean isFull(){
return usedSize== elem.length;//數(shù)組中元素的個(gè)數(shù)等于數(shù)組的長(zhǎng)度
}
private void resize(){
elem=Arrays.copyOf(elem,2*elem.length);//第二個(gè)參數(shù)為拷貝元素長(zhǎng)度,如果超出原始數(shù)組的長(zhǎng)度則補(bǔ)充默認(rèn)值,如int型則補(bǔ)充0
}
//判斷報(bào)中是否還有某個(gè)元素
public boolean contain(int toFind){
for (int i = 0; i <this.usedSize; i++) {
if(elem[i]==toFind){
return true;
}
}
return false;
}
//查找某個(gè)元素對(duì)應(yīng)的下標(biāo)
public int indexOf(int toFind){
for (int i = 0; i <this.usedSize; i++) {
if(elem[i]==toFind){
return i;
}
}
return -1;
}
//獲取pos位置的數(shù)據(jù)
public int get(int pos){
if(!checkPos(pos)){
throw new PosOutBoundsException("get 位置不合法");
}
return elem[pos];
}
private boolean checkPos(int pos){
if(pos<0||pos>=usedSize){
return false;
}
return true;
}
//獲取順序表的長(zhǎng)度
public int size(){
return this.usedSize;
}
//給pos位置設(shè)置為value,為更新數(shù)據(jù)的意思
public void set(int pos,int value){
if(!checkPos(pos)){
throw new PosOutBoundsException("set 位置不合法");
}
this.elem[pos]=value;
}
//在pos位置新增元素
public void add(int pos,int data){
if(pos<0||pos>this.usedSize){
throw new PosOutBoundsException("add新增 位置不合法");
}
if(isFull()){
resize();
}
//pos位置后的數(shù)據(jù)后移一位
for (int i =this.usedSize-1; i>=pos; i--) {
this.elem[i+1]=this.elem[i];
}
//存
this.elem[pos]=data;
this.usedSize++;
}
//刪除第一次出現(xiàn)的數(shù)字key
public void remove(int toRemove){
if(isEmpty()){
return;
}
int index=indexOf(toRemove);
if(index==-1){
return;//沒(méi)有這個(gè)數(shù)字
}
for (int i =index; i <this.usedSize-1; i++) {
this.elem[i]=this.elem[i+1];
}
this.usedSize--;
}
public boolean isEmpty(){
return this.usedSize==0;
}
//清空順序表
public void clear(){
this.usedSize=0;
}
}
Test1.java
class Test35{
public static void main(String[] args) {
SeqList seqList=new SeqList();
seqList.add(1);
seqList.add(2);
seqList.add(3);
seqList.add(4);
seqList.add(5);
seqList.display();
System.out.println(seqList.contain(5));
System.out.println(seqList.indexOf(3));
System.out.println(seqList.indexOf(7));
/* System.out.println(seqList.size());
try{
seqList.set(15,9);
} catch (PosOutBoundsException e) {
e.printStackTrace();
System.out.println("我捕獲到了一個(gè)異常");
}*/
seqList.display();
seqList.add(4,890);
seqList.display();
seqList.remove(890);
seqList.display();
}
}
ArrayList簡(jiǎn)介
ArrayList是以泛型方式實(shí)現(xiàn)的,使用時(shí)必須要先實(shí)例化
ArrayList底層是一段連續(xù)的空間,并且可以動(dòng)態(tài)擴(kuò)容,是一個(gè)動(dòng)態(tài)類(lèi)型的順序表
也可以使用如下創(chuàng)建一個(gè)對(duì)象
ArrayList的構(gòu)造
ArrayList常見(jiàn)操作
subList方法,會(huì)改變?cè)瓉?lái)對(duì)象中0位置處的數(shù)據(jù),截取拿到的是地址
ArrayList的遍歷
ArrayList 可以使用3種方式遍歷:for循環(huán)+下標(biāo)、foreach、使用迭代器
ArrayList的擴(kuò)容機(jī)制
按照1.5倍方式擴(kuò)容
如果用戶(hù)需要擴(kuò)容大小 超過(guò) 原空間1.5倍,按照用戶(hù)所需大小擴(kuò)容
ArrayList的具體使用
簡(jiǎn)單的洗牌算法
運(yùn)行結(jié)果如下
全部代碼
Card
public class Card {
//結(jié)合成為一張牌
private String suit;//花色
private int rank;//大小
public Card(String suit, int rank) {//構(gòu)造方法
this.suit = suit;
this.rank = rank;
}
//設(shè)置和獲得一張牌的花色和大小
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
@Override
public String toString() {
return "【" +
suit +
", " + rank +
'】';
}
}
Test
public class Test {
//設(shè)置花色,用于初始化所有牌
private static final String[] SUITS={"?","?","?","?"};
//初始化所有牌
public static List<Card> buyCard(){
List<Card> cards =new ArrayList<>();
for (int i = 0; i <SUITS.length ; i++) {
for (int j = 1; j <=13; j++) {
Card card=new Card(SUITS[i],j );
cards.add(card);
}
}
return cards;
}
//洗牌 從最后一張牌開(kāi)始到倒數(shù)第二張牌,隨機(jī)的與前面的某一張牌交換
public static void shuffle(List<Card> cards){
Random random =new Random();//new了一個(gè)用于產(chǎn)生隨機(jī)數(shù)的對(duì)象
for (int i =cards.size()-1; i>0 ; i--) {
int j=random.nextInt(i);//產(chǎn)生[0,i)之間的隨機(jī)數(shù)
Card temp=cards.get(i);
cards.set(i,cards.get(j));
cards.set(j,temp);
}
}
public static void main(String[] args) {
List<Card> cards =buyCard();
System.out.println(cards);
shuffle(cards);
System.out.println(cards);
//3個(gè)人,每個(gè)人輪流揭5張牌
//每個(gè)人最后會(huì)得到5張牌,我們用hand1,hand2,hand3來(lái)存儲(chǔ)每個(gè)人的5張牌
//怎么用來(lái)表示每個(gè)人呢,這里我們用hand表示,在hand順序表中,每個(gè)元素都是一個(gè)人
//而每個(gè)人都有一個(gè)順序表hand1(2,或者3)
List<Card> hand1=new ArrayList<>();
List<Card> hand2=new ArrayList<>();
List<Card> hand3=new ArrayList<>();
List<List<Card>> hand=new ArrayList<>();
//將hand1,hand2,hand3添加到hand順序表中
hand.add(hand1);
hand.add(hand2);
hand.add(hand3);
//發(fā)牌
for (int i = 0; i <5; i++) {
for (int j = 0; j <3; j++) {
//拿走最上面的一張牌
Card card=cards.remove(0);
hand.get(j).add(card);
}
}
System.out.println("第1個(gè)人得到的牌");
System.out.println(hand1);
System.out.println("第2個(gè)人得到的牌");
System.out.println(hand2);
System.out.println("第3個(gè)人得到的牌");
System.out.println(hand3);
System.out.println("剩余的牌");
System.out.println(cards);
}
}
利用ArraryList構(gòu)造出楊輝三角(采用直角三角形的樣式)
同3個(gè)人玩撲克牌一樣,這里我們也構(gòu)造出一個(gè)二維的順序表
用ret來(lái)存儲(chǔ)所有的行,list用來(lái)存儲(chǔ)每一行的元素
運(yùn)行結(jié)果如下文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-674040.html
ArrayList的問(wèn)題及思考文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-674040.html
到了這里,關(guān)于數(shù)據(jù)結(jié)構(gòu)(Java實(shí)現(xiàn))-ArrayList與順序表的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!