Solved

4D array of references to strings

Posted on 1998-04-01
6
364 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:colind
  • 5
6 Comments
 
LVL 6

Accepted Solution

by:
gadio earned 200 total points
ID: 1233928
colind, try this (filename D4Array.java):

--------


import java.util.*;

class D4Array {
  Hashtable all_data;
  Object[][][][] pointers;

  D4Array( int d1, int d2, int d3, int d4 ) {
      pointers = new Object[d1][d2][d3][d4];
      all_data = new Hashtable();
  }

  public void putElement( int d1, int d2, int d3, int d4, Object elem ) {
      if( all_data.containsKey(elem) ) {
        pointers[d1][d2][d3][d4] = all_data.get(elem);
      }
      else {
        all_data.put( elem, elem );
        pointers[d1][d2][d3][d4] = all_data.get(elem);
      }
  }

  public Object getElement( int d1, int d2, int d3, int d4 ) {
      return  pointers[d1][d2][d3][d4];
  }

  public int getNumberOfActualObjects() {
      return all_data.size();
  }
 
  public static void main( String argv[] ) {
      // a test thing...
      D4Array thetextclass = new D4Array(7,7,7,7);
      thetextclass.putElement(1,2,3,4,new String("1234") );
      System.out.println( "data:"+(String)thetextclass.getElement(1,2,3,4) );
      System.out.println( "Size:"+thetextclass.getNumberOfActualObjects() );

      thetextclass.putElement(1,1,3,4,new String("1134") );
      System.out.println( "data:"+(String)thetextclass.getElement(1,1,3,4) );
      System.out.println( "Size:"+thetextclass.getNumberOfActualObjects() );
      
      thetextclass.putElement(1,3,3,4,new String("1234") );
      System.out.println( "data:"+(String)thetextclass.getElement(1,3,3,4) );
      System.out.println( "Size:"+thetextclass.getNumberOfActualObjects() );

      thetextclass.putElement(2,2,3,4,new String("1134") );
      System.out.println( "data:"+(String)thetextclass.getElement(2,2,3,4) );
      System.out.println( "Size:"+thetextclass.getNumberOfActualObjects() );
      
      thetextclass.putElement(3,2,3,4,new String("3234") );
      System.out.println( "data:"+(String)thetextclass.getElement(3,2,3,4) );
      System.out.println( "Size:"+thetextclass.getNumberOfActualObjects() );

      thetextclass.putElement(4,2,3,4,new String("1234") );
      System.out.println( "data:"+(String)thetextclass.getElement(4,2,3,4) );
      System.out.println( "Size:"+thetextclass.getNumberOfActualObjects() );
      
  }

}


------
If you run it alone (java D4Array) you can see the small test program works.
The interface is simple. The class I think you should use (as in the small example) is the Hashtable. the average access complexity is a bit higher then o(1). You store the actual objects (note that this data structure is generic - not only for strings) in the hash and therfore you can check in o(1) time is this object (String for you) was used before. I'll add a comment about the references later.

Hope that helps for now.
G.
0
 
LVL 6

Expert Comment

by:gadio
ID: 1233929
Oops - sorry about the indentation. Copy paste thing...  
:)

0
 
LVL 6

Expert Comment

by:gadio
ID: 1233930
 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).

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:colind
ID: 1233931
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
0
 
LVL 6

Expert Comment

by:gadio
ID: 1233932
 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.

0
 
LVL 6

Expert Comment

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

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now