第三章列表

2021-08-21 03:51:30 字數 2634 閱讀 5739

listsize(屬性)    列表的元素個數

pos  (屬性)       列表的當前位置

length (屬性)     返回列表中元素的個數

clear (方法)      清空列表中的所有元素

find   (方法)       在列表中查詢某一元素

tostring (方法)   返回列表的字串形式

getelement (方法) 返回當前位置的元素

insert (方法)     在現有元素後插入新元素

remove (方法)     從列表中刪除元素

front (方法)      將列表的當前位置設移動到第乙個元素

end (方法)        將列表的當前位置移動到最後乙個元素

prev(方法)        將當前位置前移一位

next (方法)       將當前位置後移一位

currpos (方法)    返回列表的當前位置

moveto(方法)      將當前位置移動到指定位置

contains (方法)     判斷給定值是否在列表中

function list()

//返回列表元素的個數

function length()

//清空列表中所有的元素

function clear()

//在列表中查詢某一元素

function find(element)

return false;

}//給列表新增元素

this.datastore[this.listsize++]=element;

return this.listsize;

}//從列表中刪除元素

function remove(element)

return false;

}//將列表的當前位置設移動到第乙個元素

function front()

//將列表的當前位置設移動到第乙個元素

function end()

//將當前位置前移一位

function prev()

}//將當前位置後移一位

function next()

names.clear();

console.log(names.tostring());//

為了展示如何使用列表,我們將實現乙個類似 redbox 的影碟租賃自助查詢系統。為了得到商店內的影碟清單,我們需要將資料從檔案中讀進來。films.txt檔案資料內容如下:

(1) the shawshank redemption(《肖申克的救贖》)

(2) the godfather(《教父》)

(3) the godfather: part ii(《教父 2》)

(4) pulp fiction(《低俗**》)

(5) the good, the bad and the ugly(《**三鏢客》)

(6) 12 angry men(《十二怒漢》 )

(7) schindler』s list(《辛德勒名單》)

(8) the dark knight(《黑暗騎士》)

(9) the lord of the rings: the return of the king(《指環王:王者歸來》)

(10) fight club(《搏擊俱樂部》)

(11) star wars: episode v - the empire strikes back(《星球大戰 5:帝國反擊戰》)

(12) one flew over the cuckoo』s nest(《飛越瘋人院》)

(13) the lord of the rings: the fellowship of the ring(《指環王:護戒使者》)

(14) inception(《盜夢空間》)

(15) goodfellas(《好傢伙》)

(16) star wars(《星球大戰》)

(17) seven samurai(《七武士》)

(18) the matrix(《黑客帝國》)

(19) forrest gump(《阿甘正傳》)

(20) city of god(《上帝之城》

讀取檔案使用到node中fs模組的函式,需要fs模組。並將上文的列表函式通過module.exports=list;暴露出去

const fs=require('fs');//呼叫fs函式庫

const list=require('./lesson2-1.js');//呼叫list

//非同步讀取

/*fs.readfile('films.txt','utf-8',function(err,data)else

});*/

//同步讀取

// let movies=fs.readfilesync('films.txt','utf-8').split("\n");

// console.log(movies);

//讀取的內容被分割成陣列後,換行符被替換成空格。空格在比較字串時有許多問題

//使用trim方法刪除每個陣列元素未尾的空格

function createarr(file)else

}

Python學習第三章 列表

列表是由一系列按特定順序排列的元素組成,可以將任何東西加入列表,其中的元素可以彼此沒有任何關係。python中通常用 表示,用逗號分開其中的元素 bicycles trek cannondale redline specialized print bicycles 這樣輸出會將包括方括號在內一起輸出...

(資料結構)第三章 列表

向量中的秩同時對應於邏輯和物理次序,而位置僅對應於邏輯次序。3.3.1 資料結構支援的操作 靜態和動態。size 和get 均可以在常數時間內完成。insert 和remove 均需要線性時間完成。鍊錶是一種典型的動態儲存結構。節點之間通過指標相互索引和訪問。列表是鍊錶結構的一般化推廣,其中的元素稱...

python第三章 列表 筆記

1.用表示列表,用 分隔元素 2.訪問列表元素用索引,如name 0 注 索引從0開始而不是1 b 用方法pop 彈出最後乙個 如 names yang chen zhou print names poped names names.pop print names print poped names...