Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Cedric

[java] Reference to integer types

This topic is 5727 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a function that must return three values. Obviously, void f(int A, int B, int C){...} won''t work. Is there any way to make them references? I know that I could wrap them inside an object, but it''s pretty annoying. Thank you, Cédric

Share this post


Link to post
Share on other sites
Advertisement
Let A,B,C be an array ABC[] and it'll work.

class MyClass
{
...
void modify (int[] ABC) {...}
...
}


Despite of this, making it as an object is the best way to do it.
Sorry to say that, but you must learn thinking in objects.

Depending of what exactly you aim is, different approaches can fit. Here are the 2 main cases:

1. If you want to change 3 values based on few or no parameters

class Data
{
int A;
int B;
int C;
public void modify() {...}
}

class MyClass
{
...
data.modify();
...
}


2. If you want to get 3 "new" values based on few or no parameters

class Data
{
int A;
int B;
int C;
public Data(...) {...}
}

class MyClass
{
...
Data data = new Data(...);
...
}


cheers



[edited by - misterX on October 19, 2002 6:42:16 PM]

Share this post


Link to post
Share on other sites
Well, you don''t have to make a separte class, simply have it return the 3 integer values inside an array. Then just grab each integer from inside the array.

int[] array = new int[3];
.
.
.
array[0] = A;
array[1] = B;
array[2] = C;
.
.
.
return array;

Share this post


Link to post
Share on other sites
Excuse me for missing the point, but...


  
#include <iostream>

using namespace std;

void someFunc(int& a, int& b, int& c)
{
a = 1;
b = 2;
c = 3;
}

int main()
{
int x = 0;
int y = 0;
int z = 0;

someFunc(x, y, z);

cout << "x = " << x << endl;
cout << "y = " << y << endl;
cout << "z = " << z << endl;

return 0;
}


??

Share this post


Link to post
Share on other sites
example:

I want a method that returns 3 integer values, but JAVA only allows one return in a method. So, I have to store my 3 integer values inside an array.


    
public int[] return3Ints()
{
int A = 1;
int B = 2;
int C = 3;

int[] array = new int[3];

array[0] = A;
array[1] = B;
array[2] = C;

return array;
}


This is much easier than having to code a new class that holds 3 integer values.

[edited by - MaximuS_X on October 19, 2002 6:55:06 PM]

[edited by - MaximuS_X on October 19, 2002 6:57:04 PM]

[edited by - MaximuS_X on October 19, 2002 8:10:08 PM]

Share this post


Link to post
Share on other sites
Uh, to atone for that, here''s something.


  
public void someFunc(int[] myArray)
{
for (int i = 0; i < myArray.length; ++i)
myArray[i] = i + 1;
}

public void doSomething() {
int[] myArray = new int[3];

myArray[0] = 0;
myArray[1] = 0;
myArray[2] = 0;

someFunc(myArray);

for (int i = 0; i < myArray.length; ++i) {
System.out.println("The number is: " + myArray[i]);
}
}


Dammit, Cédric, you posted here just to trick me, admit it! Stick to C++ in future .

Share this post


Link to post
Share on other sites
quote:
Original post by Alimonster
Dammit, Cédric, you posted here just to trick me, admit it! Stick to C++ in future .

That''ll teach you to use the Active Topics page! Lazy GameDever!

Thanks for the response, everyone. I already knew about the array work-around, but I don''t find it very elegant. "Thinking in objects" sounds more like "Living with only one arm and one leg".

I miss C++ already

Cédric

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!