Generic Arrays
Author |
Message |
copthesaint
|
Posted: Mon Jan 18, 2010 1:34 pm Post subject: Generic Arrays |
|
|
I was just playing around with generics for the fun of it and developed this code:
code: | public class GenDeclairSetup <V> {
private V[][] v;
private int[][] code;
private int getID (){
int returnIndex = 0;
for (int i = 0; i != (0-1); i = i + 1){
if (this.v.length < i || code[i][0]!= 10000 + i){
this.code[i][0] = 10000 + i;
returnIndex = i;
i = -1;
}
}
return returnIndex;
}
private int getID (int indexID){
int returnIndex = 0;
for (int i = 0; i != (0-1); i = i + 1){
if (this.v[indexID].length < i || code[indexID][i]!= 10000 + indexID){
this.code[indexID][i] = 10000 + i;
returnIndex = i;
i = -1;
}
}
return returnIndex;
}
private void remove (int index){
for (int i = 0; i < this.code[index].length; i = i + 1){
this.code[index][i] = 0;
}
}
private int create (V v,int index){
int returnValue = getID (index);
this.v[returnValue][index] = v;
return returnValue;
}
public int create (V v){
int returnValue = getID ();
this.v[returnValue][0] = v;
return returnValue;
}
public int createArray (V v, int size){
int returnValue = getID ();
for (int i = 0; i < size; i = i + 1){
this.v[returnValue][getID (returnValue)] = v;
}
return returnValue;
}
public void clr (int index){
remove (index);
}
public void clrAll (){
for (int i = 0; i < this.code.length ;i = i + 1){
remove (i);
}
}
} |
It compiles correctly, but somehow, I just dont think it will work.. Lol I also had tested it just to see if I could create 1 value holder. And As I thought it doesnt work but still please comment on this and maybe how you know how to make it work.
Also here is the code I used to test it:
code: | public class TestGen {
public static void main (String[] args){
GenDeclairSetup<Integer> g = new GenDeclairSetup<Integer>();
int batman = g.create (4124);
}
} |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
DemonWasp
|
Posted: Mon Jan 18, 2010 3:00 pm Post subject: RE:Generic Arrays |
|
|
If nothing else, you need to allocate those arrays you've declared (note that it's spelled "declare", not "declair"). You also need to provide some documentation on what this is and why it's useful, because right now it just looks to me like you've got a silly version of an ArrayList.
You should probably also use meaningful method names. Why "clr" instead of "clear"? |
|
|
|
|
|
copthesaint
|
Posted: Mon Jan 18, 2010 8:34 pm Post subject: RE:Generic Arrays |
|
|
1st I dont think it will be to usefull to anyone Lol I am just trying to learn Generics. And also why not clr? Apparently you had gotten it Lol. But I do know what your saying. and what are you saying about allocating the arrays? |
|
|
|
|
|
chrisbrown
|
Posted: Tue Jan 19, 2010 10:28 am Post subject: RE:Generic Arrays |
|
|
You don't have a constructor for GenDeclairSetup, you should make one and initialize your v and code arrays in it (not allocate, this isn't C/++) |
|
|
|
|
|
DemonWasp
|
Posted: Tue Jan 19, 2010 7:45 pm Post subject: RE:Generic Arrays |
|
|
Yes "allocate". The new keyword in Java will allocate some portion of the RAM that the JVM has pre-allocated itself for this purpose. Initializing refers to setting the initial value of a variable before accessing its value - both are done in this case.
I understand clr because that's a relatively common shortening used in older languages, some of which I'm familiar with. There's no reason to use it over clear() now. Which would you rather see used in code?
What might be more useful is a class like Pair<T>, which would allow you to return a pair of items from a function (alternately, Tuple<T>, which allows you to return some number of values; similar to a List but not exactly the same). |
|
|
|
|
|
andrew.
|
Posted: Tue Jan 19, 2010 8:51 pm Post subject: RE:Generic Arrays |
|
|
If JVM handles the memory and garbage collection, what is the point of allocating and deallocating memory? Doesn't JVM do this itself? Maybe I'm missing something :/ |
|
|
|
|
|
DemonWasp
|
Posted: Tue Jan 19, 2010 10:50 pm Post subject: RE:Generic Arrays |
|
|
In this case, the allocation isn't telling the operating system to allocate RAM to your program, it's telling the JVM to allocate RAM to your program. The JVM has already allocated the RAM from the OS. Allocation is required to specify the amount of RAM you need and to get a pointer to that RAM in return.
The term "allocation" just means "take some part of this pool of (resource) and give it to me". In this case, the pool belongs to the JVM, not the OS. |
|
|
|
|
|
andrew.
|
Posted: Wed Jan 20, 2010 5:21 pm Post subject: RE:Generic Arrays |
|
|
Yeah, but doesn't JVM handle this on its own? Or does it ask for more memory from the OS when you "allocate"? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
chrisbrown
|
Posted: Wed Jan 20, 2010 6:53 pm Post subject: Re: RE:Generic Arrays |
|
|
DemonWasp @ Tue Jan 19, 2010 7:45 pm wrote: Yes "allocate".
My mistake.
andrew. wrote: Yeah, but doesn't JVM handle this on its own? Or does it ask for more memory from the OS when you "allocate"?
The JVM is given a chunk of memory from the OS on startup. Variables are allocated from that chunk. I don't know if the JVM expands as needed, but I know you can specify how much memory it requests on startup. |
|
|
|
|
|
DemonWasp
|
Posted: Thu Jan 21, 2010 2:07 am Post subject: RE:Generic Arrays |
|
|
The JVM will keep a bounded amount of memory in reserve (having allocated it from the OS). If you allocate more than it has in the pool, it will allocate more from the OS up to its maximum (or run the garbage collector). If you allocate enough to exceed its maximum, new allocations will through OutOfMemory exceptions (an unchecked exception).
The JVM will also release RAM to the OS if, after some length of time, your program doesn't seem to need more than X, down to some minimum.
You can control the initial pool, the minimum and the maximum, using command-line switches (-Xmx and -Xms as I recall). It may also be possible to change those at runtime, but I don't know for sure. |
|
|
|
|
|
|
|