Link to home
Start Free TrialLog in
Avatar of colind
colind

asked on

4D array of references to strings

I'm fairly new to Java and I need a little help with a very specific problem...  

I need to create a 4D array datatype with the last dimension holding strings.  In Turbo pascal it might look like

joe : array[1..7,1..6,1..60,1..168] of String;

In fact, these are the exact dimensions I need but that doesn't really matter.  Now, knowing that this question is too easy for all you guys, I have something else that I need help with.

Instead of just holding strings, I'd like this structure to hold references to strings in order to save memory.  The reason is this...  Throughout this whole array, there might only be as few as 100 distinct strings but they all get copied a couple hundred times so that they each fill 100 array elements at a time.  Instead of duplicating the strings in memory every time I copy them into a new array element, I'd like to just store references to those strings.  That way, each element of the array will take 4 bytes (or whatever a Java reference takes) instead of 30 odd bytes for the string, hence saving mondo memory.

So, here's what I need...

I need a class with a constructor that when instantiated, will create this 4D array of whatever data type is necessary to store references to strings.  I'd like to be able to specify the dimensions by passing parameters, if that's ok...  So, this class would be instantiated like this, I think...

4dArray joe = new 4dArray(5,6,30,168); //how do I access the individual elements?

I'll also need a few basic instructions on how references work in Java since I haven't been able to find any information on them...  Even in the Sunsoft Tutorial (am I blind?).  If somebody knows of another good URL where one can get good info on Java, I'd be very much appreciative.  I'm familiar with references in PERL ( \variable-name ) and pointers in Pascal but I don't know if that's how Java works at all.  Once I have that information, I think I'll be able to write my methods, etc.  Oh, if you need a length for the strings, set them at 30.

Thank you,

Colin
ASKER CERTIFIED SOLUTION
Avatar of gadio
gadio

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gadio
gadio

Oops - sorry about the indentation. Copy paste thing...  
:)

 colind, I must add that if you refer to a location in the array that wasn't assigned with a value, you will get null.
  Now, regarding your question, java doesn't have references at all. Java only deals with pointers, and the pointers can only be pointers to classes (not to the primitive types as possible in C,C++). So you can achieve the reference effect by using the pointer of a class. Also, the pointers functionality is different than in C,C++. The 'dangerous' powers that the pointers had (example - wild casting between pointer types, pointer arithmetic etc.) doesn't exist in java, and other new features were added. In general - you haven't found the information because its not there. What you do have are pointers. If you know pointers in C its a good start. In the small example above, none of the structures (not the Hashtable nor the 4D array) really hold the actual data. Both of them holds Object pointers (Object the the most basic class in java) to the actual object that can be found somewhere in the JVM allocated memory. By the way, the JVM countsthe number of live pointers to every object, and when its 0, this object is a candidate of being deleted (garbage collection).

Avatar of colind

ASKER

Great answer gadio!  This looks like it does exacty what I need.  I have a couple questions just to clarify a couple things...

I noticed the "(string)" in this line:

System.out.println( "data:"+(String)thetextclass.getElement(3,2,3,4) );

Does this mean that when I want to extract an element from the array structure, I have to specify what kind of object it is?  Is this the case all the time?  For instance, say I was storing integers in the array, would I do an addition like:

Joe = 5 + (int)thetextclass.getElement(3,2,3,4);

??

Also, you said that all unassigned values are null...  Is there a way that I could test for that???  I'll give you the context of what I mean...  

Assume I have two arrays of strings with dimension [200].  30 elements in each array are filled with strings or pointers or whatever.  I want to take array1 and copy every non-null element into the corresponding element of Array2 and then test array2 to see if it contains 60 non-null elements or whether it contains less than 60 (because some were overwritten).  When I make the method to do this, can I do something like...

for (joe = 0; joe < 201; joe++) {
  if (Array1[joe]) {
     Array2[joe] = Array1[joe];
  }
}

Would that work?

Thanks again for the great and speedy response,

Colin
 The (String) cast wasn't necessary at that case but I did it deliberately so that you can see how to use this data structure. The thing is that since I did this data structure general, any class can be stored is it (all classes in java are derived from the class Object) so when you get an element you have to cast it to the type that it is. However, in java all the inheritance data is stored in the pointer type (the new features I reminded above) so you can query an object about its type for example:

Object ob = thetextclass.getElement(3,2,3,4);
if( ob instanceof String ) {
    String str = (String)ob;
    // whatever...
    // whatever...
}
else if( ob instanceof MyClass ) {
    MyClass m = (MyClass)ob;
    // whatever...
    // whatever...
}

This feature is almost unique to java (SmallTalk has it to) and a most powerful one. By  he way, the example that you gave will actually wont work because the int type cant have  pointer to it (as all other basic types - I monitored it). You could have use the Integer (or is it Int?) class instead. The Integer class  that wraps an int value in a class. That is  somewhat confusing at the beginning but quite easy once you get the idea of the java thing.

  Regarding your other question, well, the example you gave was almost right. The right version would be:

     for (joe = 0; joe < 201; joe++) {
       if (Array1[joe] != null ) {
          Array2[joe] = Array1[joe];
       }
     }

Its quite simple. 'null' is a constant in java (its not coded as 0 like in C). BUT, you wouldn't want to copy arrays this way anyway in java since you can use the clone method, for example:

    String[] ar1;
    String[] ar2;
    // whatever...
    ...
    // whatever...
    ar2 = ar1.clone();

Now that looks much more simple isn't it? (I haven't tested this piece of code but I as far as I remember its correct - use the java api documentation for the exact usage).

Regards,
G.

 Sorry, there are some mistakes there. Its not my first language and I wrote this fast so...
(eg: 'monitored' was originally 'mentioned')
:-}