Question about arrays

Richard A. O'Keefe ok at atlas.otago.ac.nz
Tue Nov 20 04:18:44 UTC 2001


"Christopher K Roach" <chrisroach at btinternet.com> wrote:
	ok, I have a RECORD with the some elements in it, does anyone
	know how to make an array of records?
	
You make an array, and you put records in it.

	Object subclass: #Record
	
	Can I do :
	
	Array subclass: #Record
	
No.  That says that the class Record is a subclass of Array.
You want an array that CONTAINS records, not an array that IS A record;
this isn't even remotely close to how you would do it in any other
object-oriented language

What you do is just
	a := Array new: numberOfElements.
	a at: 1 put: ("your first record goes here").
	...
	a at: numberOfElements put: ("your last record goes here").

	I think you have to made record a subclass of an array?  however I dont know
	how to reference the array is you know what I mean?

You are wasting your time sending messages to this mailing list.
Grab a copy of one of the tutorials (ANY of the tutorials) at once,
and read it.

	a := Array new: n.	"create an array with n elements, 1..n"
	a at: index put: value.	"set a[index] := value"
	stuff := a at: index.	"set stuff := a[index]"


	If i know the maximum
	array to be 10,000 I could create 10,000 array BUT that is soo inefficient !
	! ! Dynamic arrays...
	
are called OrderedCollections.

	a := OrderedCollection new.
	... a size ...		"number of elements in a right now"
	a add: x.		"add x at end of a"
	a at: i put: y.		"change an existing element"
	... a at: i ...		"fetch an existing element"

But there's more.  Much much more.

	Also I have a file to read in from of the following form:
	
	element1,element2,element3,element4 CR
	repeated 10,000 times....
	
	readFile
	
	
	        Smalltalk at: #f put: (FileStream
	            open: 'test.data'
	            mode: FileStream read) !
	
	
	
	           [f atEnd] whileFalse: [
	
	                         a addelement1: f upTo: $, .

You have a method called addelement1:upTo: somewhere?




	                         a addfirst: f upTo: $, .
	                         a addage: f upTo: $, .
	                         a addheight: f upTo: Character cr ]
	
This should presumably be something like
		[f atEnd] whileFalse: [
		    |title name ref number|
		    title := f upTo: $,.
		    name := f upTo: $,.
		    ref := f upTo: $,.
		    number := f upTo: Character cr.
		    a add: (Record title: title name: name
				   ref: ref number: number)]
assuming that your Record class has an appropriate creation method.





More information about the Squeak-dev mailing list