Skip to content

Commit 1d6064b

Browse files
authored
修复 checkOutOfRange 函数和一些相应的优化
修复 checkOutOfRange 函数 优化 insert 函数 修改和 checkOutOfRange 函数相关的逻辑判断
1 parent cee7915 commit 1d6064b

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

php/05_array/array.php

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public function checkIfFull()
5252
*/
5353
private function checkOutOfRange($index)
5454
{
55-
if($index > $this->length+1) {
56-
return true;
55+
if ($index >= $this->length) {
56+
return true;
5757
}
5858
return false;
5959
}
@@ -68,18 +68,16 @@ public function insert($index, $value)
6868
{
6969
$index = intval($index);
7070
$value = intval($value);
71-
if($index < 0) {
71+
if ($index < 0) {
7272
return 1;
7373
}
74-
if($this->checkIfFull()) {
74+
75+
if ($this->checkIfFull()) {
7576
return 2;
7677
}
77-
if($this->checkOutOfRange($index)) {
78-
return 3;
79-
}
8078

81-
for($i=$this->length-1;$i>=$index;$i--) {
82-
$this->data[$i+1] = $this->data[$i];
79+
for ($i = $this->length - 1; $i >= $index; $i--) {
80+
$this->data[$i + 1] = $this->data[$i];
8381
}
8482

8583
$this->data[$index] = $value;
@@ -96,22 +94,21 @@ public function delete($index)
9694
{
9795
$value = 0;
9896
$index = intval($index);
99-
if($index < 0) {
97+
if ($index < 0) {
10098
$code = 1;
101-
return array($code, $value);
99+
return [$code, $value];
102100
}
103-
if($index != $this->length+1 && $this->checkOutOfRange($index)) {
101+
if ($this->checkOutOfRange($index)) {
104102
$code = 2;
105-
return array($code, $value);
103+
return [$code, $value];
106104
}
107105

108106
$value = $this->data[$index];
109-
for($i=$index;$i<$this->length-1;$i++) {
110-
$this->data[$i] = $this->data[$i+1];
107+
for ($i = $index; $i < $this->length - 1; $i++) {
108+
$this->data[$i] = $this->data[$i + 1];
111109
}
112-
113110
$this->length--;
114-
return array(0, $value);
111+
return [0, $value];
115112
}
116113

117114
/**
@@ -123,23 +120,23 @@ public function find($index)
123120
{
124121
$value = 0;
125122
$index = intval($index);
126-
if($index < 0) {
123+
if ($index < 0) {
127124
$code = 1;
128-
return array($code, $value);
125+
return [$code, $value];
129126
}
130-
if($this->checkOutOfRange($index)) {
127+
if ($this->checkOutOfRange($index)) {
131128
$code = 2;
132-
return array($code, $value);
129+
return [$code, $value];
133130
}
134-
return array(0, $this->data[$index]);
131+
return [0, $this->data[$index]];
135132
}
136133

137134
public function printData()
138135
{
139136
$format = "";
140-
for($i=0;$i<$this->length;$i++) {
141-
$format .= "|".$this->data[$i];
137+
for ($i = 0; $i < $this->length; $i++) {
138+
$format .= "|" . $this->data[$i];
142139
}
143-
print($format."\n");
140+
print($format . "\n");
144141
}
145-
}
142+
}

0 commit comments

Comments
 (0)