Smalltalk将一个Sorted集合写入文件 [英] Smalltalk write a Sorted Collection to a file

查看:127
本文介绍了Smalltalk将一个Sorted集合写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为 whitepages 的SortedCollection,每个记录包含一个 Customer c> name 和 number
我想创建一个函数,以这种方式将它们写入文件

let's say i have a SortedCollection called whitepages, each record containing a Customer, with values name and number. I want to create a function which would write them to a file in such way

name1 
number1 
name2
number2

我必须承认Im完全停留在这里。有人可以帮助吗?

I have to admit Im completely stuck here. Could anyone help?

推荐答案

好的,所以我希望你知道如何写入文件。
当你注意到你已经排序集合,我假设 Customer 按你想要的方式排序。

Ok, so I hope you know how to write to file. And as you noticed that you have sorted collection I suppose that Customers are ordered in the way you want.

然后你可以做的是:

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

方式你会得到一个字符串,你只需要写入文件。还要注意,如果 name number 不是字符串,您可以使用 asString

This way you'll get a string that you just need to write to the file. Also note that if name or number are not strings you can use asString on them.

规范化的方式是执行类似的操作:

The canonical way is to do something like:

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

是写入文件的流。

这篇关于Smalltalk将一个Sorted集合写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆