Skip to content

Commit 1163863

Browse files
Merge pull request wangzheng0822#105 from MGLEE-RUSH/master
数组的Python实现
2 parents 9d64c82 + fcd19d5 commit 1163863

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

python/array.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# 1.数组的插入、删除、按照下标随机访问操作;
2+
# 2.数组中的数据类型是Int
3+
#
4+
# Author:Lee
5+
6+
class Array():
7+
8+
def __init__(self):
9+
'''数组类初始化方法.'''
10+
self.__data = [] # 数据存储List
11+
12+
def find(self, index):
13+
'''数组的查找方法.
14+
15+
参数:
16+
index:将要查找的数据的下标
17+
18+
返回:
19+
如果查找成功,则返回找到的数据
20+
如果查找失败,则返回False
21+
'''
22+
if index > len(self.__data) or index < 0:
23+
return False
24+
else:
25+
return self.__data[index]
26+
27+
def delete(self, index):
28+
'''数组的删除方法.
29+
30+
参数:
31+
index:将要删除的数据的下标
32+
33+
返回:
34+
如果删除成功,则返回True
35+
如果删除失败,则返回False
36+
'''
37+
if index > len(self.__data) or index < 0:
38+
return False
39+
else:
40+
self.__data.pop(index)
41+
return True
42+
43+
def insert(self, index, value):
44+
'''数组插入数据操作.
45+
46+
参数:
47+
index:将要插入的下标
48+
value:将要插入的数据
49+
50+
返回:
51+
如果插入成功,则返回True
52+
如果插入失败,则返回False
53+
'''
54+
if index > len(self.__data) or index < 0:
55+
return False
56+
else:
57+
self.__data.insert(index, value)
58+
return True
59+
60+
def insertToTail(self, value):
61+
'''直接在数组尾部插入数据.
62+
63+
参数:
64+
value:将要插入的数据
65+
'''
66+
self.__data.append(value)
67+
68+
def printAll(self):
69+
'''打印当前数组所有数据'''
70+
print(self.__data)

0 commit comments

Comments
 (0)