![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Hobbyist Programmer
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3
![]() |
When you are done with this, if you wouldn't mind sending me the code I'd like to look at it. I still think you should use Serialization. Since I can't compile it, I would probably write some buggy code.
![]() |
|
|
|
|
|
#12 | |
|
Expert Programmer
|
Quote:
|
|
|
|
|
|
|
#13 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
another error from code
Another problem in my code is that I'm not able to send array information between classes. I've created a sample program: Would anyone be able to fix this program so that it actually returns the array?
In c++, I would just send a pointers/refferencea to my array and into the class method. In java is there anyway I can have a pointer to my array in ArrayCollect, and bring that pointer into ArrayData. Or does this work with by fixing the syntax errors? Edit**
class ArrayData
{
int someInt [] = new int [1];
public ArrayData ()
{
someInt = new int [3];
for (int i = 0 ; i < 3 ; i++)
{
someInt [i] = (5 * i) + 1;
}
}
int getInt ()
{
return someInt [];
}
}
class ArrayCollect
{
public static void main (String [] arguments)
{
ArrayData tempData = new ArrayData ();
int yeint[] = new int[3];
yeint[] = tempData.getInt ();
}
}
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#14 |
|
Professional Programmer
|
int getInt ()
{
return someInt ;
}
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#15 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Small correction:
int[] getInt()
{
return someInt;
} |
|
|
|
|
|
#16 |
|
Professional Programmer
|
Thanks Arevos, I should pay more attention damnit.
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#17 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Thanks for all your help. Somethings in my mind though: In c++ you can pass pointers and refferences into a funtion. So in java is there any simple way to do this? I searched the forums and found "event methods", wow I don't want to get into that. Therefore, this can't be done as it can in c++ can it (create a refference to an object)?
Here's a little example I Found. #include <iostream>
#include <stdlib.h>
void swap(int &x, int &y);
int main()
{
int x = 5, y = 10;
std::cout << "Main. Before swap, x: " << x
<< " y: " << y << "\n";
swap(x,y);
std::cout << "Main. After swap, x: " << x
<< " y: " << y << "\n \n \n";
int x2 = 30, y2 = 40;
std::cout << "Main. Before swap, x: " << x2
<< " y: " << y2 << "\n";
swap(x2,y2);
std::cout << "Main. After swap, x: " << x2
<< " y: " << y2 << "\n";
system("Pause");
return 0;
}
void swap (int &rx, int &ry)
{
int temp;
std::cout << "Swap. Before swap, rx: " << rx
<< " ry: " << ry << "\n";
temp = rx;
rx = ry;
ry = temp;
std::cout << "Swap. After swap, rx: " << rx
<< " ry: " << ry << "\n";
}Quote:
Now back to finishing this tetris game.
__________________
Death smiles at us all. All a man can do is smile back. Last edited by Eric the Red; Jun 5th, 2006 at 8:36 PM. |
|
|
|
|
|
|
#18 |
|
Hobbyist Programmer
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3
![]() |
This is a horribly complicated subject for me to explain, but I'll try.
If I understand you correctly, you want to pass the array and modify it and not have to return it. In Java, you can do this the same way as C++. The references/pointers you pass in Java/C++ pass by value. They are numbers. Just for sake of an example: char *temp = new char[x];
foo( temp );
void foo( char *ptr )
{
ptr = new char[y];
}I'll just show you a simplified example with sample addresses. It would probably help if you kept track on paper. char *temp; temp = new char[x]; the value of the array's address (0x10) is stored in 0x00 (temp) foo( temp ); ptr = new char[y]; the value of the array's address (0x20) is stored in 0x04 (ptr) The chart would look like this: location value 0x00 0x10 0x04 0x20 0x10 the value of temp[0] 0x20 the value of ptr[0] Put another way, it is like saying this: int temp = 1; int ptr = temp; ptr = 100; Temp won't change, just ptr. char * is just an int that holds a memory address. (on 32-bit machines) |
|
|
|
|
|
#19 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Thanks.. my book doesn't cover any of that stuff (Sam's Java Programming in 24 Hours). I'm definately out to get a book tonight.
Great explanation!
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#20 |
|
Programmer
Join Date: May 2006
Posts: 39
Rep Power: 0
![]() |
lol yeah, sams 24 hour books have a bad reputation...
all that stuff *should* be explained in any book claiming to teah java progamming. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|