returning 3 items

Hi,I need a method that can return 2 integers and 1 stringthe return can only have 1 item.Cai I put this 3 items of a different type in one array an d retunr just that array?THX
[219 byte] By [ugie] at [2007-9-26 1:47:52]
# 1

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?

ugie at 2007-6-29 2:47:27 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2

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.

npirs at 2007-6-29 2:47:27 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3
thx for your help, but with this object I can only return 3 values, but I want an objectarray, so I can return different times 3 values,for example obj[1].int1obj[1].int2obj[1].string1
ugie at 2007-6-29 2:47:27 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 4

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.

DanielBakkelund at 2007-6-29 2:47:27 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 5

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

jmschrei at 2007-6-29 2:47:27 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 6
THX jmschrei,That was something I was looking for, but can you also tell me how I can add these elements to the vector using a loop, cause I don't know how many elements that have to be put in the vector, it will be dynimacally generatedTHX a LOT!!!!!!!!
ugie at 2007-6-29 2:47:27 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 7
if you are from a c / C++ background , then you should know that objects are passed by reference.When you want more than one variables to be passed from a function as a return type then just call the object as an argument to the said function..
chandansharma at 2007-6-29 2:47:27 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 8

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;

}

}

jmschrei at 2007-6-29 2:47:27 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 9
THX, you' ve resolved my problem!!But I can't return a vector can I?I can not do public Vector Getsomeitems(parameterX)Vector myVector = new Vector(xxxx);return myVector;or is it possible?
ugie at 2007-6-29 2:47:27 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 10
You do not have to return the Vector at all.Your class can be, and should probably be, void.If you pass the reference of your Vector to the methodthen your Vector is being acted upon directly.
GLJdotcom at 2007-6-29 2:47:27 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 11
I'm sure GLJdotcom meant:The return type of your method can be, and should probably be, void.Rich
rmiller1985 at 2007-6-29 2:47:27 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 12
You seei'm calling this method from a JSP pagethis JSP should be able to import the vector so I can use a loop within this JSP page in order to get a nice HTML pageHow can I have access to the vector I created in that method? THX
ugie at 2007-6-29 2:47:27 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 13

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;

}

amishslayer at 2007-6-29 2:47:27 > top of Java-index,Archived Forums,New To Java Technology Archive...