Reputation: 311
Hi I made up this class to add to string arrays and remove elements from them. I also did one that prepares a group of values for writing to a csv file. However I would like to do it so that it will take in any value (for the first two methods as the last one will always be a string) How do I make it Generic so? And ps I know I shouldnt send the two dimension arguments for the csv builder but I only found out how to get the row dimension now.:)
public class ManipulateList {
public static String [] removeFromArray(String []old_array,int index) {
String []old_vals = old_array;
int old_length= old_vals.length;
int new_length= old_length-1;
String[] new_vals = new String[new_length];
System.arraycopy(old_vals, 0, new_vals, 0, index);
if(index+1!=old_length) {
System.arraycopy(old_vals, index+1, new_vals,index,old_length-1-index);
}
return new_vals;
}
public static String[] addToArray(String []old_array,String element) {
String new_element=element;
String[] old_vals = old_array;
int old_length= old_vals.length;
int new_length= old_length+1;
String[] new_vals = new String[new_length];
System.arraycopy(old_vals, 0, new_vals, 0, old_length);
new_vals[old_length]=new_element;
return new_vals;
}
public static String[] createCsvList(String[][] lists, int contents1,int contents2){
String[][] csvarray=new String[contents1][contents2];
csvarray = lists;
String[]newcsv=new String[contents2];
StringBuilder newline = new StringBuilder();
for (int i = 0; i<contents2;i++) {
for (int j = 0; j<contents1;j++) {
newline.append(csvarray[j][i]);
if(contents1-j>1) {
newline.append(",");
}
}
newcsv[i]=newline.toString();
newline= new StringBuilder();
}
return newcsv;
}
}
Upvotes: 2
Views: 103
Reputation: 23113
Pretty much, replace String[] with T[] and you're done.
Here's the first one. You'll notice that I mostly just replaced String[]
with T[]
and added <T>
to the method signature.
public static <T> T[] removeFromArray(T[] old_array,int index)
{
T[] old_vals = old_array;
int old_length = old_vals.Length;
int new_length = old_length - 1;
@SuppressWarnings("unchecked") T[] new_vals = (T[]) Array.newInstance(old_array.getClass().getComponentType(), new_length);
System.arraycopy(old_vals, 0, new_vals, 0, index);
if(index + 1 != old_length)
{
System.arraycopy(old_vals, index+1, new_vals, index,old_length - 1 - index);
}
return new_vals;
}
Edit:
Apparently, to create a new array, it has to be done like this:
public T[] createArray(Class<T> cls, int size) throws Exception
{
@SuppressWarnings("unchecked") T[] array = (T[])Array.newInstance(cls, size);
return array;
}
Upvotes: 3
Reputation: 3241
It would be better to use a List, or better yet, an object containing a List, instead of a String[]. Then you would not have to write your own methods to add elements to arrays.
If you really want to use arrays for some reason, it's fairly difficult, since the following is illegal:
T[] myNewList = new T[];
The compiler does not have the information at runtime to know what T is when you're calling the method (due to type erasure). You can fake it in various ways such as using ArrayList.toArray(), but I'd just avoid the problem entirely and use Lists, as mentioned above.
However, for CSV, I'm not sure why you would want anything except for Strings anyway. CSV files are effectively lists of lists of strings.
Upvotes: 1