JCoster
September 23rd, 2009, 12:51 PM
Hi guys,
Just a quick question.
I currently have a main class in Java defined as follows:
import java.util.ArrayList();
public class Graph {
private ArrayList<Vertex> exampleList;
public static void main(String[] args) {
// create the graph instance
new Graph();
}
public Graph() {
exampleList = new ArrayList<Vertex>;
// Populate the list as an example
exampleList.add(new Vertex("example1"));
exampleList.add(new Vertex("example2"));
}
public Integer getValue(Vertex tempVertex) {
return exampleList.get(tempVertex);
}
public void printMessage() {
System.out.println("Added successfully.");
}
}
This is just an example... I actually want to do something more meaningful than what I'm about to say BUT, from creating each vertex, can I call the printMessage() function from within the Vertex class?
For example:
public class Vertex {
private String value;
public Vertex(String value) {
this.value = value;
/*
* Now I want to call the 'printMessage()' method from this
* class.
*/
}
}
Of course I know I could put the printMessage() method inside the Vertex class but really I want to do a method which can return a vertex from the ArrayList to the Vertex.
So it's actually more a case of the printMessage() method being:
public Vertex returnVertex(String value) {
Vertex tempVertex = ..get vertex through some code and
the Value parameter..
/*
* Where temp vertex is the vertex I've selected from some code
* above.
*/
return tempVertex;
}
Actually, I guess a simpler way to put this is that from each vertex in the ArrayList, I would like to be able to call a method in the Graph class from the Vertex class which allows me to get a different Vertex into the original Vertex instance.
And yes, I'm very bad at explaining things...
Just a quick question.
I currently have a main class in Java defined as follows:
import java.util.ArrayList();
public class Graph {
private ArrayList<Vertex> exampleList;
public static void main(String[] args) {
// create the graph instance
new Graph();
}
public Graph() {
exampleList = new ArrayList<Vertex>;
// Populate the list as an example
exampleList.add(new Vertex("example1"));
exampleList.add(new Vertex("example2"));
}
public Integer getValue(Vertex tempVertex) {
return exampleList.get(tempVertex);
}
public void printMessage() {
System.out.println("Added successfully.");
}
}
This is just an example... I actually want to do something more meaningful than what I'm about to say BUT, from creating each vertex, can I call the printMessage() function from within the Vertex class?
For example:
public class Vertex {
private String value;
public Vertex(String value) {
this.value = value;
/*
* Now I want to call the 'printMessage()' method from this
* class.
*/
}
}
Of course I know I could put the printMessage() method inside the Vertex class but really I want to do a method which can return a vertex from the ArrayList to the Vertex.
So it's actually more a case of the printMessage() method being:
public Vertex returnVertex(String value) {
Vertex tempVertex = ..get vertex through some code and
the Value parameter..
/*
* Where temp vertex is the vertex I've selected from some code
* above.
*/
return tempVertex;
}
Actually, I guess a simpler way to put this is that from each vertex in the ArrayList, I would like to be able to call a method in the Graph class from the Vertex class which allows me to get a different Vertex into the original Vertex instance.
And yes, I'm very bad at explaining things...