-2

I am trying to create a depth first search algorithm here, kind of. Instead of traversing and finding out how many values it had to go through, it should be finding the highest value in each row.

I know how to actually implement the search and everything but the one part I can't figure out, especially since googling it doesn't work very well is how to create an array of Sub Classes/childs.

I don't really need to know how to initialize a general array, but rather how to initialize an array when I have a class of Node with 2 int values, and then I want an array of said Nodes with the 2 values in them

e.g. right now I have

  Node Node1 = new Node(1,1);
    Node Node2 = new Node(2,3);
    Node Node3 = new Node(2,2);
    Node Node4 = new Node(3,5);
    Node Node5 = new Node(3,3);
    Node Node6 = new Node(3,9);

I want to use an array where it would be something like Node[0] = 1,1 or something similar to that - is there any way this could be done in Java?

Cœur
  • 32,421
  • 21
  • 173
  • 232
David Li
  • 37
  • 5
  • `node[0] = new Node(1,1)`? – takendarkk Aug 31 '17 at 21:13
  • Look into [ArrayList](https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html) – nyvokub Aug 31 '17 at 21:14
  • Possible duplicate of [How do I declare and initialize an array in Java?](https://stackoverflow.com/questions/1200621/how-do-i-declare-and-initialize-an-array-in-java) – Izruo Aug 31 '17 at 21:24
  • Depth First Search implies a tree structure. Do you plan on creating a tree? You talk about arrays and rows, so that is confusing. There are a lot of tutorials around on how to create a tree, and how to perform a DFS on it - that would probably be a good place to start. Then you can modify your DFS so that it behaves in the manner you describe. – Matt Sep 01 '17 at 00:44
  • @Izruo I wish what I was looking for was that easy :/ – David Li Sep 01 '17 at 00:58

1 Answers1

0

Something like:

Node[] nodes = {new Node(1,1), new Node(2,3)};

Then you could say something like:

nodes[0] = new Node(5,5);

or

nodes[0].setXValue = 1 // Or whatever this method and value would be

etc. But look at the comments also as you could use an ArrayList for more flexibility or use another way of initializing an array as in the link given.

Michael Burke
  • 103
  • 1
  • 9
  • thanks! exactly what I was looking for, google doesn't exactly work when I search for class arrays or object arrays :) – David Li Sep 01 '17 at 00:58