Posted 20 April 2013 - 09:04 AM
This intended for the experienced Java programmer, I want to show my Java code that manipulates arrays and I gladly want to improve it. However, one thing: I know its better to use arraylists, but this is just some practise for my Java programming skills.
Well here is my "array-util" code:
You could say I have no experience in Java programming, so thats why I made this.
I would love some feedback on how to improve things, or even feedback to let me know this is good :P/>
Thank you in advance,
- Engineer
Well here is my "array-util" code:
public String[] removeFrom(String[] array, int index)
{
String[] newArray = new String[array.length - 1];
int newIndex = 0;
for(int i = 0; i < array.length; i++)
{
if(i != index)
{
newArray[newIndex] = array[i];
newIndex++;
}
}
array = null;
return newArray;
}
//Set index to -1 to add it at the end
public String[] addItem(String[] array, String item, int index)
{
String[] newArray = new String[array.length + 1];
int realIndex = 0;
for(int i = 0; i < array.length; i++)
{
if(i == index)
{
newArray[i] = null;
} else {
newArray[i] = array[realIndex];
realIndex++;
}
}
if(index == -1)
{
newArray[array.length + 1] = item;
} else {
newArray[index] = item;
}
array = null;
return newArray;
}
public String[] replaceIndex(String[] array, int index, String newString)
{
String[] newArray = new String[array.length];
for(int i = 0; i < array.length; i++)
{
if(i == index)
{
newArray[i] = newString;
} else {
newArray[i] = array[i];
}
}
array = null;
return newArray;
}
You could say I have no experience in Java programming, so thats why I made this.
I would love some feedback on how to improve things, or even feedback to let me know this is good :P/>
Thank you in advance,
- Engineer