I would like to have something like a structure in C language
there I could define a structure with one string and two integers, the I could search the structure like
structure.int1
structure.int2
structure.string
is there something like a structure in JAVA?
You will need to create a serialized object. eg:
public class SerialObj implements java.io.Serializable{
public int int1, int2;
public String str1;
public SerialObj(int first, int second, String third){
this.int1=first;
this.int2=second;
this.str1=third;
}
}
Compile this seperately and you can use it in any program you desire, this is similar to structures in C.eg:
SerialObj obj = new SerialObj(1,2,"three");
then when it is returned to where ever you may print out the elements like so:
System.out.println("Integer 1: "+obj.int1);
System.out.println("Integer 2: "+obj.int2);
System.out.println("String: "+obj.str1);
hope this works for you.
The fastest solution is to place the results in a HashMap object and just return this.
However you will loose all type-checks made by the compiler (as you would if you returned an Object[] ).
The type-safe way is to create a "data-object" that contains getters and setters for all the values you return:
public class Data
{
private int _int;
private float _float;
private String _string;
public void putFloat( float f )
{
_float = f;
}
public float getFloat()
{
return _float;
}
// etc....
}
The class need not implement Serializable unless you intend to pass it across a network (RMI) or stream it to disc.
You might want to try something like this:
public class MyStructure {
private int x,y;
private String str;
// your only constructor
MyStructure(int x, int y, String str) {
this.x = x;
this.y = y;
this.str = str;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public String getStr() {
return str;
}
}
// now create your Vector of MyStructure
Vector myVector = new Vector();
MyStructure aStructure = new MyStructure(7,10, "Hi!");
MyStructure bStructure = new MyStructure(1,6, "Bye!");
myVector.addElement(aStructure);
myVector.addElement(bStructure);
// etc.
MyStructure currentStruct;
for (int i = 0; i < myVector.size(); i++) {
// you are casting the element in the vector of objects
// to be of class MyStructure
currentStruct = (MyStructure)myVector.elementAt(i);
// you can access any of the methods of MyStructure
// now.
int xValue = currentStruct.getX();
int yValue = currentStruct.getY();
int strValue = currentStruct.getStr();
}
let me know if you want more info
jmschrei
boolean complete = false;
Vector myVector = new Vector();
MyStructure myStructure = null;
int x,y;
String theString;
while (!complete) {
// read in x or dynamically get it
// read in y or dynamically get it
// read in theString or dynamically get it
myStructure = new MyStructure(x, y, theString);
myVector.addElement(myStructure);
if (you determine you are finished) {
complete = true;
}
}
returning a vector works just fine, and is right way to do this if you are not passing the vector into the fuinction
public Vector yourFunction(yourParameters){
Vector aVector = new Vector(); (unless it is already visable from the scoping or it was passed in as a parameter)
//
//build and modify your vector
//
return aVector;
}