尝试遍历 Eiffel 中的数组时出错
Error trying to traverse an array in Eiffel
我得到一个错误unknown identifier 'start'
我的代码:
visit_table(table: ETABLE)
local
t_array: ARRAY[ARRAY[ELEMENT]]
b_header: BOOLEAN
row_index: INTEGER
do
t_array := table.t_array
b_header := table.b_header
table.content := "<table>"
row_index := 0
from t_array.start
until t_array.off
loop
table.content := table.content + "<tr>"
from t_array.item.start
until t_array.item.off
loop
if row_index = 0 and b_header = TRUE then
table.content := table.content + "<th>" + t_array.item.content + "</th>"
else
table.content := table.content + "<td>" + t_array.item.content + "</td>"
end
end
table.content := table.content + "</tr>"
row_index := row_index + 1
end
table.content := table.content + "</table>"
end
我只想解析二维数组的对象并在它们周围包裹 html 标签。
我的语法有问题吗?
class ARRAY
不继承 TRAVERSABLE
提供的功能 start
、after
、forth
和 item
.因此,不能使用这些特性来遍历数组。相反,遍历通常使用从 lower
开始并在 upper
结束的项目索引来完成。因此,在您的代码中将有两个类型为 INTEGER
的变量 i
和 j
(一个用于内循环,另一个用于外循环)和
之类的循环
from
j := t_array.lower
until
j > t_array.upper
loop
... t_array [j] ... -- Use item at index `j`.
j := j + 1 -- Advance to the next element of the array.
end
另一种方法是使用循环的迭代形式:
across
t_array as x
loop
... x.item ... -- Use current item of the array.
end
后一种方法更接近于您当前代码中使用的方法,因此您可能更喜欢它。请注意,在这种情况下,无需调用 start
、off
和 forth
,因为这是自动完成的。
最后,可以使用您当前的代码,但要迭代 linear_reprentation
。为此,您需要另外 2 个局部变量 p
和 q
,类型分别为 LINEAR [ARRAY [ELEMENT]]
和 LINEAR [ELEMENT]
。第一个将使用 p := t_array.linear_representation
初始化,外部循环将使用 p.start
、p.after
、p.item
和 p.forth
。第二个将用 q := p.item.linear_representation
初始化。这是最麻烦的方法,我列出它只是为了完整性。
我得到一个错误unknown identifier 'start'
我的代码:
visit_table(table: ETABLE)
local
t_array: ARRAY[ARRAY[ELEMENT]]
b_header: BOOLEAN
row_index: INTEGER
do
t_array := table.t_array
b_header := table.b_header
table.content := "<table>"
row_index := 0
from t_array.start
until t_array.off
loop
table.content := table.content + "<tr>"
from t_array.item.start
until t_array.item.off
loop
if row_index = 0 and b_header = TRUE then
table.content := table.content + "<th>" + t_array.item.content + "</th>"
else
table.content := table.content + "<td>" + t_array.item.content + "</td>"
end
end
table.content := table.content + "</tr>"
row_index := row_index + 1
end
table.content := table.content + "</table>"
end
我只想解析二维数组的对象并在它们周围包裹 html 标签。
我的语法有问题吗?
class ARRAY
不继承 TRAVERSABLE
提供的功能 start
、after
、forth
和 item
.因此,不能使用这些特性来遍历数组。相反,遍历通常使用从 lower
开始并在 upper
结束的项目索引来完成。因此,在您的代码中将有两个类型为 INTEGER
的变量 i
和 j
(一个用于内循环,另一个用于外循环)和
from
j := t_array.lower
until
j > t_array.upper
loop
... t_array [j] ... -- Use item at index `j`.
j := j + 1 -- Advance to the next element of the array.
end
另一种方法是使用循环的迭代形式:
across
t_array as x
loop
... x.item ... -- Use current item of the array.
end
后一种方法更接近于您当前代码中使用的方法,因此您可能更喜欢它。请注意,在这种情况下,无需调用 start
、off
和 forth
,因为这是自动完成的。
最后,可以使用您当前的代码,但要迭代 linear_reprentation
。为此,您需要另外 2 个局部变量 p
和 q
,类型分别为 LINEAR [ARRAY [ELEMENT]]
和 LINEAR [ELEMENT]
。第一个将使用 p := t_array.linear_representation
初始化,外部循环将使用 p.start
、p.after
、p.item
和 p.forth
。第二个将用 q := p.item.linear_representation
初始化。这是最麻烦的方法,我列出它只是为了完整性。