簡體   English   中英

Smalltalk將Sorted Collection寫入文件

[英]Smalltalk write a Sorted Collection to a file

假設我有一個名為whitepages ,每個記錄包含一個Customer ,其值分別為namenumber 我想創建一個將它們以這種方式寫入文件的函數

name1 
number1 
name2
number2

我必須承認我完全被困在這里。 有人可以幫忙嗎?

好的,我希望您知道如何寫入文件。 正如您所注意到的,您已經對集合進行了排序,我想按需要的方式對Customer進行排序。

然后,您可以做的是:

(whitepages collect: [ :customer |
  customer name,
  Character cr asString,
  customer number ]) joinUsing: Character cr

這樣,您將獲得只需要寫入文件的字符串。 還要注意,如果namenumber不是字符串,則可以在其上使用asString

規范的方法是執行以下操作:

whitepages do: [ :customer |
  stream
    nextPutAll: customer name;
    cr;
    nextPutAll: customer number;
    cr ]

stream是文件的寫流。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM