Alice Community  

Go Back   Alice Community > Alice 2 > How do I...?

Reply
 
Thread Tools Display Modes
Find length of an array of any type?
Old
DickBaldwin
Guest
 
Status:
Posts: n/a
Default Find length of an array of any type? - 04-15-2007, 12:01 PM

In Alice 2.0, if I drop an Object[] array variable on a new line in the edit pane, the menu that appears on the screen is shown on the left below. This is what I would expect. If I drop an array variable of other types on the new line in the edit menu, the last line is missing from the menu that appears but the first two lines are still there. This is also what I would expect.

If I drop an Object[] array variable on a placeholder of type Number in an expression in the edit pane, the menu that appears on the screen is still the one shown on the left below just as if I had dropped it on a new line in the edit pane.

That is not what I would expect, and quite frankly it doesn't make any sense to me. I would have expected, as a minimum, to see a function that returns the number of elements in the array. This is also true if I do the same thing with a String[] array variable or a Boolean[] array variable. It is probably also true for other types other than Number[] as well, but I haven't tested all possible types.

If I do the same thing with a Number[] array variable, the menu that appears on the screen is shown in the image on the right below. I would expect the first line in that menu to appear only for the case of an array of type Number[], but I would expect the second line to be there regardless of the type of data stored in the array.

I have a vague recollection of having seen an Alice menu item somewhere that returns the length of an array, which is Java's name for the size of an array, but I don't remember where it was, and I can't find it now.

I accidentally discovered that I can solve the problem by first constructing the statement to get the length of the array using a dummy array of type Number[] as a placeholder and then replacing the reference to the dummy array by a reference to another array of a different type, but that doesn't seem right to me. That requires me to declare a dummy array variable of type Number whether I need it or not.

Where am I going wrong on this? Any clarification as to the correct procedure for getting the size of an array of any type would be appreciated.

Thanks,
Dick Baldwin
Attached Images
File Type: jpg alice0185a01.jpg (5.6 KB, 699 views)
File Type: jpg alice0185a06.jpg (3.0 KB, 692 views)

Last edited by DickBaldwin; 04-15-2007 at 12:03 PM. Reason: Clarify references to images. Originally referred to file names.
   
Reply With Quote
More questions than answers.
Old
DrJim
Guest
 
Status:
Posts: n/a
Red face More questions than answers. - 04-16-2007, 03:24 PM

You will occassionally see something like "arrrayname.lenght" when you are working in the Java listing format but this seems to be very inconsistent - as are arrrays in general (see the attached example which I have yet to figure out).
Attached Files
File Type: a2w Misc_test.a2w (221.6 KB, 359 views)
   
Reply With Quote
Old
DickBaldwin
Guest
 
Status:
Posts: n/a
Default 04-16-2007, 05:32 PM

Well, I got stumped by the declaration of the variable of type Transformable.

I have never been able to figure out, or find any information about the purpose of the Transformable type. Can you provide some info on this or point me to some written material on it.

Thanks,
Dick Baldwin
   
Reply With Quote
Transformable
Old
DrJim
Guest
 
Status:
Posts: n/a
Default Transformable - 04-16-2007, 06:18 PM

Sorry, about the transformable - that was left over from some of my original testing. What has me stumped in particular in that example is why I can't "say" elements of the numeric array as strings.

As far as I can tell, the "Transformable" data type is identical to the object data type - note the "Java-like" printout lists both as objects, even though the little symbol is actually different for the true object. I've never seen anything in print about them.

I had thought it had something to do with the "is First Class" option, a "Seldom Used Property" which I actually use a lot (only first class objects can be selected with the mouse) - but if there ever was a connection, it doesn't still seem to be there. Nor do there seem to be any tranform methods that only work for transformables - though I certainly haven't tried all possibilities.

(Also had hoped for a group of transformer library objects - either the toys or the real things - but no luck there either. )

Jim
   
Reply With Quote
Old
DickBaldwin
Guest
 
Status:
Posts: n/a
Default 04-16-2007, 09:17 PM

You wrote:

"What has me stumped in particular in that example is why I can't "say" elements of the numeric array as strings."

The issue there is the complexity surrounding the process of setting one array to another array. See my discussion of this at

http://www.dickbaldwin.com/alice/Alice0180.htm

Apparently, when you declare an array of one (first) type and then "set" it to another array (second) of a different type, you change the type and possibly the size also of the first array to match that of the second array. Then you populate the elemens in the first array with references to the contents of the elements in the second array, losing whatever may originally have been stored in the first array.

See the attached modified version of your program where I successfully "say" the contents of the numeric array before setting that array to the second array. At that point, I believe that the original contents of the numeric array have been destroyed, and in fact the type has been effectively changed from Number[] to String[]. If the two arrays had been different sizes, even the size of the first array would be changed. See the attached a2w file.

Frankly this all seems very strange to me because of my Java background. Assuming that a "set" command in Alice is comparable to an assignment in Java, it would not be possible to set one array of a given type to another array of a different type in Java.

More correctly, all array structures are encapsulated in objects in Java, and you must store a reference to an array object in a reference variable with a matching type (not exactly correct, but close enough) in order to access the contents of the array.

While you could create another array object of the same type and store its reference in the original reference variable (overwriting what was originally there) you could not create a new array object of a different (incompatible) type and store its reference in the original reference variable. And, the primitive numeric types are definitely not compatible with the string type.

I would like to see an example of a program that sets one Alice array to another Alice array and follows good programming practice. So far, I haven't been able to come up with any good use for the ability to set one array to another array.

By the way, on a totally different subject, what is the "proper" way for including a quote from a posting in the response to that posting?

Dick Baldwin
Attached Files
File Type: a2w RgbMisc_test.a2w (222.8 KB, 322 views)
   
Reply With Quote
Old
DrJim
Guest
 
Status:
Posts: n/a
Default 04-16-2007, 11:28 PM

Quote:
Originally Posted by DickBaldwin View Post
Frankly this all seems very strange to me .....
That makes two of us. Think I will just stick with my practice of just avoiding arrays in Alice, but thanks for the information.

Quote:
By the way, on a totally different subject, what is the "proper" way for including a quote from a posting in the response to that posting?
Don't know how to write it without actually doing the formatting, but if by "proper" you mean the formatting, it's just done with html-like codes in square brackets. Just hit the small quote button below a message to get the basic structure - then you can edit and copy as you want. (I usually have to use preview a couple of times.)
   
Reply With Quote
Old
DickBaldwin
Guest
 
Status:
Posts: n/a
Default 04-17-2007, 10:02 AM

Quote:
Originally Posted by DrJim View Post
Don't know how to write it without actually doing the formatting, but if by "proper" you mean the formatting, it's just done with html-like codes in square brackets. Just hit the small quote button below a message to get the basic structure - then you can edit and copy as you want. (I usually have to use preview a couple of times.)
Now I understand. You hit the quote button instead of the reply button. I was hitting the reply button first and then trying to find the quote button.

Thanks,
Dick Baldwin
   
Reply With Quote
Old
DickBaldwin
Guest
 
Status:
Posts: n/a
Default 04-17-2007, 10:16 AM

Quote:
Originally Posted by DrJim View Post
That makes two of us. Think I will just stick with my practice of just avoiding arrays in Alice, but thanks for the information.
I can't tell my students to stay away from arrays in general, because that is a fundamental programming concept. However, I will tell them to stay away from the set value method as it applies to arrays. That seems to be the thing that produces the unusual behavior.

Dick Baldwin
   
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Copyright ©2023, Carnegie Mellon University
Alice 2.x © 1999-2012, Alice 3.x © 2008-2012, Carnegie Mellon University. All rights reserved.