*
The moose likes Beginning Java and the fly likes Arrays and Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays and Collections" Watch "Arrays and Collections" New topic
Author

Arrays and Collections

Lou Bassett
Greenhorn

Joined: Nov 08, 2004
Posts: 13
I heard this forum was an great site for beginners as myself, thank you in advance for anyone who helps out.

I have recently migrated from 20 years of as400/RPGIV/ILE programming to Java. One project involves displaying real-time attributes of service-type people stored on the AS/400 in a *USRSPC(UserSpace). When a user signs on, they get a 277-byte chuck of that space. In the AS/400 world, the entire *USRSPC is overlaid into a "multiple occurance data structure"(array) so that each element of the array is a service person and the data on them is automatically mapped into the data structure.

It made sense to me to use the same approach in Java, however, may sources have advised me to use a "Collection" instead. I've looked at the documentation and it is confusing.

I need a simple example or a link to the same.

Sorry for the lengthy message but, I wanted to be specific.

Thanks


Lou Bassett
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
This may take a few emails for me to get a better understanding of what you need.

You are already familar with arrays, right?

OK, well, it sounds to me (maybe) like you need a class to represent your data.
Something like this maybe:




And now inside of your application, you need to collect this data in an array.

First you instanciate and array:



And later on you add data to your array like this:




The '4' here is just for illustration purposes.
Lou Bassett
Greenhorn

Joined: Nov 08, 2004
Posts: 13
Wow, thanks for the quick response!

The userspace is 16MB and could hold alot of users @/277 bytes each. My main confusion is what's the difference between an array and a collection? I want to load a dynamic number of service people (it can change second to second), differentiate which supervisor they report to and accumulate group and individual stats. Do I need to load an array with everyone and use the collection class' methods on it?

Thanks
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
Think of the array as a type of collection.

Don't think of it as having a fixed size. Consider it to be dynamic. The add() method will manage the elements for you.

Don't think of it as supporting only primitive types (int doubles char, etc).

My example used an array of objects of type "ServicePeople".

It's still up to you as the programmer to manage the data in your array (managers, statistics, etc). By the array itself is very simple to use.

Do you have an example handy or do you need a simple one?
Lou Bassett
Greenhorn

Joined: Nov 08, 2004
Posts: 13
I would appreciate a sample. thanks
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
Here it is.

Just copy and past it into a "testing" package:

Jimmy Die
Ranch Hand

Joined: Nov 20, 2003
Posts: 97
Hi,

One of the ways that I look at arrays is that their size (the array's size) is fixed. They also have ramdom access to a specific element.

"Collection classes offer more flexible ways to access an object, and unlike arrays, can expand or contract dynamically as you add or remove elements."

Different Collection classes have different features that make them special, just as an array has features that make it special. Some collections sort fast, or no duplicates...

Hope that this helps...


Jimmy Die
Harry Wood
Greenhorn

Joined: Jan 28, 2004
Posts: 16
Which of the Collections should I be using in the following situation?

I have a bunch of Strings, each with an assoctiated int ID.

I want it to have ordering. i.e. I add items in a particular order, and then retreive them in that same order (this rules out Hashtable)

I could create a diddy little class with two member variables: int ID, and the String. Then I add instances of this class to a Vector (or ArrayList). However this is such crappy little class definition, for such a common problem. I dont want to clutter my project with a new class definition. I mean what do I call a class containing just an int and String? Is their built in java class for a pair of Objects perhaps.

A second approach would be to make two Vectors. One for the ids and one for the Strings. This is clearly not best practice, since the values could become missaligned, if I add to one Vector, but forget to add to the other for example. But for such a simple thing, I am tempted by this bad approach

My problem is that this chunk of code should really look and feel very very simple, and yet I dont see a java Collection which caters for it.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Perhaps a HashMap will suffice. The Map type in Java associates a key with a value. In this case, I think they key can be your integer id, as an Integer wrapper object, and the String can be the value.

This is just a shot in the dark since I'm not entirely clear on how this data will be used. You should look at the API docs to see if this particular Collection will meet your needs.

Layne

p.s. A previous post seemed to imply that an array is the same as ArrayList, but they are not. For one thing an array is not a Collection where an ArrayList. I'd rather not get into the details as you can find more information by searching this forum or googling for it. If you need more clarification, please ask.


Java API Documentation
The Java Tutorial
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Harry Wood:
Which of the Collections should I be using in the following situation?

I have a bunch of Strings, each with an assoctiated int ID.

I want it to have ordering. i.e. I add items in a particular order, and then retreive them in that same order (this rules out Hashtable)
You want to store String/int pairs in the same order that you added them. That leaves you with a List of some sort. However, you didn't mention anything about how you need to access them, unless add-order (index) is the only access you need.

Yes, you could creat a Pair class, or use Map.Entry (inner class of Map with two Object members: key and value). I'd go the former and make it generic, just to clarify your design.

If you want to use two Lists, I'd recommend creating a class that manages them together. It would provide a List-like interface:
  • void add(String, int)
  • String getString(int)
  • int getInt(String)
  • void remove(int) // you could return String or int removed, but not both

  • Creating a class to manage the lists will allow you to be confident that you don't accidently add the String but forget the int. Encapsulation will save you countless hours of debugging.
    Rick Portugal
    Ranch Hand

    Joined: Dec 17, 2002
    Posts: 243
    Originally posted by jefff willis:
    Think of the array as a type of collection.

    Don't think of it as having a fixed size. Consider it to be dynamic. The add() method will manage the elements for you.
    I just want to point out that an array is different than an ArrayList. An array does have a fixed size:

    An array is not a collection, but an ArrayList is. An ArrayList does not have a fixed size and can grow as big as you need it.


    IBM 286, SCJP, SCWCD, EIEIO
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Arrays and Collections
     
    Similar Threads
    why we need Map[Hashtable or hashMap]?
    please clear this off
    data structures and databases
    Data Structure used by Java array.
    Question for all.