forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request wangzheng0822#105 from MGLEE-RUSH/master
数组的Python实现
- Loading branch information
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# 1.数组的插入、删除、按照下标随机访问操作; | ||
# 2.数组中的数据类型是Int | ||
# | ||
# Author:Lee | ||
|
||
class Array(): | ||
|
||
def __init__(self): | ||
'''数组类初始化方法.''' | ||
self.__data = [] # 数据存储List | ||
|
||
def find(self, index): | ||
'''数组的查找方法. | ||
参数: | ||
index:将要查找的数据的下标 | ||
返回: | ||
如果查找成功,则返回找到的数据 | ||
如果查找失败,则返回False | ||
''' | ||
if index > len(self.__data) or index < 0: | ||
return False | ||
else: | ||
return self.__data[index] | ||
|
||
def delete(self, index): | ||
'''数组的删除方法. | ||
参数: | ||
index:将要删除的数据的下标 | ||
返回: | ||
如果删除成功,则返回True | ||
如果删除失败,则返回False | ||
''' | ||
if index > len(self.__data) or index < 0: | ||
return False | ||
else: | ||
self.__data.pop(index) | ||
return True | ||
|
||
def insert(self, index, value): | ||
'''数组插入数据操作. | ||
参数: | ||
index:将要插入的下标 | ||
value:将要插入的数据 | ||
返回: | ||
如果插入成功,则返回True | ||
如果插入失败,则返回False | ||
''' | ||
if index > len(self.__data) or index < 0: | ||
return False | ||
else: | ||
self.__data.insert(index, value) | ||
return True | ||
|
||
def insertToTail(self, value): | ||
'''直接在数组尾部插入数据. | ||
参数: | ||
value:将要插入的数据 | ||
''' | ||
self.__data.append(value) | ||
|
||
def printAll(self): | ||
'''打印当前数组所有数据''' | ||
print(self.__data) |