Menu

#22 ArrayUtil :: Array to Stack, Queue and List

0.1a
closed
7
2005-06-05
2005-02-02
eRom
No

Hi,

In the ArrayUtil class we can build a new SimpleStack
from an Array instance. Good !
But it can be useful to build another data type from an
array, like Queue, ArrayList for exemple.

I try a small implementation to see the utility of that
thing, and I think is quite useful in project
development, when we work with lots of data types.
I think about an overload method that build new
instance of data type passed in arguments chains.....
For exemple :
----------------------------------------------------
public static function toHolder(array:Array, newHolder,
order:Boolean) {
var overload:Overload = new Overload(eval("th"+"is"));
overload.addHandler([Array, Stack, Boolean], _toStack);
overload.addHandler([Array, Queue, Boolean], _toQueue);
overload.addHandler([Array, List, Boolean],
_toArrayList);
return overload.forward(arguments);
}

private static function _toArrayList(array:Array,
list:List, order:Boolean) : List {
var len:Number = array.length;
if (!order) {
for (var i:Number = len-1; i >= 0; i--) {
list.insert(array[i]);
}
} else {
for (var i:Number = 0; i < len; i-=-1) {
list.insert(array[i]);
}
}
return list;
}
----------------------------------------------------

Thus elsewhere we can have this :
----------------------------------------------------
var myArray:Array = new Array("Romain", "ECARNOT",
"aka", "eRom");

var testC:List = ArrayUtils.toHolder(myArray, new
ArrayList(), true);
testC.insertLast("OK");
trace("Holder ArrayList = " + testC);
----------------------------------------------------

Rapid and practical to pass from a type to another
isn't it ?

Perhaps we can force simple type like SimpleStack,
LinearQueue and ArrayList instead of general Stack,
Queue and List...to prevent instanciation of TypedQueue
and TypeStack class witch require another way of
contruction..

Thanks.
eRom.

Discussion

  • eRom

    eRom - 2005-02-05
    • priority: 5 --> 7
     
  • Simon Wacker

    Simon Wacker - 2005-06-05

    Logged In: YES
    user_id=922171

    Hi eRom,
    your idea about a possible solution is great. The problem is
    that there will be a dependency from ArrayUtil to every other
    data holder, which increases the file size a lot. I thus used
    another solution.
    You can pass an array to every data holder on construction
    and the data holder will populate itself with the contents of
    this array. I also added a method to get an array
    representation of every data holder (toArray). This means
    when you want to convert data holders, you can just do
    something like this:

    var myList:List = new ArrayList([1, 2, 3, 4]);
    var myStack:Stack = new SimpleStack(myList.toArray());

    I think that this solution is also good and easy to use and
    does not cause unnecessary dependencies.

    Cheers,
    Simon

     
  • Simon Wacker

    Simon Wacker - 2005-06-05
    • status: open --> closed
     
  • eRom

    eRom - 2005-06-05

    Logged In: YES
    user_id=1196309

    Yes sure, good and easy.
    'toArray' method is really useful in this case ;)

    Thanks.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.