嗨,我组成了这个类来添加到字符串数组并从中删除元素。我还做了一个准备一组值以写入 csv 文件的方法。但是我想这样做,以便它接受任何值(对于前两种方法,因为最后一种方法总是一个字符串)我如何使它成为通用的呢?并且 ps 我知道我不应该为 csv 构建器发送二维参数,但我现在才知道如何获取行维度。:)
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;
}
}