   [java] C++ to Java - A few more questions
  Hi everyone. Thanks for the help with previous questions.I have been reading Sun's Tutorial a bit more and loads more questions have cropped up.What is the Java equivalent of this activity?In a class in c++ you would set up a array like so:// Declared in a class__int32 m_sizeOfArray;__int32 *m_pArray;These would be set to 0 and NULL. Then later in the program I would dynamically set these in a function:BOOL G_SetArray(__int32 &sizeofArray, __int32 **ppArray){// Size of Array is conditionally setif(something is true) sizeofArray = 10;else if(something else is true) sizeofArray = 20;else sizeofArray = 30;*ppArray = new __int32[sizeofArray];memset(*ppArray, 0, sizeofArray * sizeof(__int32)); // Dont worry about memset, I have worked out a nice utility function to rapidly mimic memset}This function would then be called somewhere in the program:G_SetArray(m_sizeOfArray, &m_pArray);Now, from what I have read in Java some porting issues will crop up.1. Just like c++ if you have a function BOOL Function(__int32 number) the function will not change the value of number in the calling function, even if it changes it in Function itself. Great stuff. But sometimes, as in the example above you want the number to actually change in the function and remain changed when it returns to the calling function. This is done using a REFERENCE:G_SetArray(__int32 &sizeofArray,...Does Java have references? If not how would this concept be replicated?(Note: I could get the function to return sizeofArray in this case but I do not want to do that. I have made this example simple. In my program I might want to set several arrays of different sizes in the same function.)2. In c++, to make sure your pointer to the array stays changed when it returns to the calling function you send the function a pointer to a pointer - ppArray:BOOL G_SetArray(__int32 &sizeofArray, __int32 **ppArray)and send the location of the pointer in the calling function.From what I have read in Java so far, I am  not absolutely certain how an array pointer would change in a function and remain changed when returning to the calling function.Is this possible in Java?(Note: again I dont want the function to return ppArray for similar reasons as above.)3. Tied in with all of the above I am having a bit of trouble getting my head around the idea of not being in control of my memory management. As a programmer I like to be in total control of my program (I have issues - don't ask). Java seems to want to usurpe this control away from me. Let me explain my problem.In the c++ example above, I might like to call my G_SetArray(m_sizeOfArray, &m_pArray) function several times in my program. Before each call I would manually cleanup m_pArray and reset it to NULL. The size of the array and the array it self will be dynamically changing throughout the entire program.Is this possible in Java? Can I reuse the array in this manner? How do I cleanup between each call?That's todays questions. Thanks for all the help so far.Jon   
  1. and 2.All parameters in Java are passed by value. Object parameters are really references to the objects on the heap (think of it like a C++ pointer to the object). You cant pass references to references.So what you need to do is: create a class that holds both your int and a reference to the array and pass an object of that class to the method. Within the method, change whatever you need, e.g. (re)initialize the array - <parameter>.setArray(new int[<size>])).Note that Java has vectors like C++. There's the old Vector class and the newer ArrayList which represent resizable arrays.Further note that Java arrays have a length attribute, so you don't need to store the size externally.3.To clear and reinitialize an array in Java you could either use a container like ArrayList and call clear() or, if you want a plain array, just reassign a new array to the reference.The old array will be garbage collected when the VM decides to (you can hint the VM it should run the GC via System.gc() but there's not guarantee it will do so [smile]).   ;  In Java you can emulate passing a pointer/reference to a function by using an array with one element in it. Consider this C++ code as example:void foo(int* i){  *i = 3;}...int k;foo(&k);In Java you could write:void foo(int[] i){  i[0] = 3;}...int[] k = new int[1];foo(k);;Quote:Note: I could get the function to return sizeofArray in this case but I do not want to do that. I have made this example simple. In my program I might want to set several arrays of different sizes in the same function.I have no idea why that changes anything, but then again I can't really understand what you are getting at. Can't you call the function on each array separately?Why don't you want to return a value?A literal translation would be something like this:public class Util{    public int [] getArray()    {   int size = determineArraySize(); // conditional stuff   // note, in Java this will be initialised to all zeros   // A call to a  memset style function is unnecessary.   return new int[ size ];    }};Thanks guys for the tips.I really like the ArrayList LordEvil.nmi, that's a good idea. It's a shame the language adds this cumbersome layer. But a clever idea.Rip-Off: Sadly I need to set these arrays simultaneously, checking multiple things all at once based on certain conditions. Otherwise the program would be continuously checking the same conditions for each individual array that needs to be set up. It would be a nightmare in programming.Lord Evil: you say ....if you want a plain array, just reassign a new array to the reference.This has raised a question in my mind.Say I have a plain array of size 3 objects.MyContainerClass{MyClass [] array;}// In a function SetUp()array = new MyClass[3];// Each of these is then set to a proper value...array[0] = MyClass(...initialiser...);array[1] = MyClass(...initialiser...);array[2] = MyClass(...initialiser...);// In another function RemoveUnwanted()// I then use these and ultimate no longer need the MyClass object array[1]// Is this legal, making sure the old array[1] can be cleaned up by the VMarray[1] = array[2];array[2] = NULL;// Or would I have to do this instead in a function RemoveUnwanted()// As a c++ programmer this gives me the heeby-geebies.MyClass [] arrayTemp = new MyClass[2];arrayTemp[0] = array[0];arrayTemp[1] = array[2];array = arrayTemp;This last option goes against everything I have learnt in C++. (I feel unclean.)Jon;Quote:Is this legal, making sure the old array[1] can be cleaned up by the VMYes, you can. Well, in Java it is "null", not NULL, but in essence you can. This does mean that you will have to check whether the elements are null before accessing them elsewhere though.If the order of the elements is irrelevant, then you can use the "swap and pop" trick that you would use on a C++ std::vector<>. This means that you would need to store the logical length of the array too. But by then you have written most of the important parts of ArrayList, and you might as well use it.   ;Quote:Original post by JonAkaJon// Is this legal, making sure the old array[1] can be cleaned up by the VMYes, it's legal. The VM can clean up the object pointed to by array[1] if there's no other reference to it (not counting isolated circular references which the gc can detect). However, there's no way to tell if the gc will clean it up.Quote:Original post by JonAkaJonThis last option goes against everything I have learnt in C++. (I feel unclean.)You should get used to the fact that Java handles many thing in a different way than C++ [grin].Like rip-off said, you can swap array entries the way you posted. However, the array is still of size 3, just the last reference being null. In C++ MyClass array[]; would roughly correspond to MyClass* array[]; (with several differences like: in Java array is an object of its own and thus can have fields like "length").Another note: In C++ copying array would require to copy the individual MyClass objects if you declared it like so: MyClass array[];In Java the copying would just involve creating a new array object and filling it with references to objects on the heap.;  Thanks guys. These are really useful comments and tips.Thanks to you guys the transfer from c++ to Java is not so daunting.