Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java Language
Expertise: Beginner
May 27, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Maintaining arrays of bindings between names and associated arrays

I have few problems with the following code:
abstract class AbstractGraph extends java.awt.Frame
   static int HighValue,LowValue,ArraySize;
   static String InstancesArray[][] = new String [20][ArraySize];
   float ReturnArray(String name)
 static void AddArray(String name,float array[])
The InstancesArray holds a name and values of arrays created by a given user.

My first problem is that I could not access the variable ArraySize from a different class. I had to make it static first; only then was I able to assign a value to it from other function.

But then the array created did not have these dimensions; instead, it gave me exceptions when I tried to input something into the array.

I also can't for some reason return an array from the ReturnArray method.

Also, I don't understand why I had to make the method AddArray static to be able to do what it is supposed to do (add name and values of arrays created by a user to the InstancesArray).

If I understand your application, you want to maintain an array of bindings between names (represented as Strings) and associated arrays of floating point numbers.

If I'm correct, your initial declaration of instanceArray:

 String instancesArray[] ...
is not correct, because it declares a two-dimensional array of Strings.

Instead, instancesArray should be declared as an array of Bindings:

 private Binding instancesArray[];
A Binding object holds a String and an associated array of floats. It also provides some useful constructors. For example:
class Binding {
   public String name;
   public float[] values;
   public Binding() {
      name = new String("void");
      values = new float[0];
   public Binding(String s) {
      name = s;
      values = new float[0];
   public Binding(String s, float[] nums) {
      name = s;
      values = nums;
In this case, here's what your returnArray method might look like:
public float[] returnArray(String name) {
      for(int i = 0; i < graphSize; i++)
         if (instanceArray[i].name.equals(name))
            return instanceArray[i].bindings;
      return new float[0];
Your next problem seems to be lack of a constructor. Of course you won't be able to instantiate your AbstractGraph class as long as it's declared abstract (I assume you had good reason to do this), so your constructor will appear in a derived class.

To simplify things, I declared Graph as a concrete class. My constructor allows users to specify the length of instancesGraph:

class Graph {

   private int arraySize, graphSize
   private Binding instancesArray[];

   public Graph(int gs) {
      graphSize = gs;
      instancesArray = new Binding[gs];
      for(int i = 0; i < 20; i++) 
         instancesArray[i] = new Binding();

   public float[] returnArray(String name) {...}

   public void addArray(String name, float[] array) {...}

My addArray is pretty kludgy. It looks for the next available space in instancesArray and modifies the binding there. You can probably think of 100 better ways to do this:
public void addArray(String name, float[] array) {
      int next = -1;
      for(int i = 0; i < graphSize; i++) 
         if (instanceArray[i].name.equals("null")) {
            next = i;
      if (next > -1) {
         instanceArray.name = name;
         instanceArray.values = array;
Finally, main() constructs a graph g, and starts adding bindings to it:
public Main {
   public static void main() {
      Graph g = new Graph(20);
      float[] vals = new float[3] = {100, 200, 300};
      g.addBinding("first", vals);
Without declaring and initializing instances of the Graph class, only the graph class itself exists as an object. The only members of a class object are those declared static.
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date