Maintaining arrays of bindings between names and associated arrays

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 ofbindings between names (represented as Strings) and associated arrays offloating 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 alsoprovides 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’tbe able to instantiate your AbstractGraph class as long as it’s declaredabstract (I assume you had good reason to do this), so your constructorwill appear in a derived class.
See also  5 Tips to Manage a Remote Workforce

To simplify things, I declared Graph as a concrete class. Myconstructor 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 ininstancesArray and modifies the binding there. You can probably think of100 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;            break;         }      if (next > -1) { = 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 thegraph class itself exists as an object. The only members of a class objectare those declared static.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist