2

嗨,我组成了这个类来添加到字符串数组并从中删除元素。我还做了一个准备一组值以写入 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;
    }
}
4

2 回答 2

3

差不多,用 T[] 替换 String[] 就完成了。

这是第一个。您会注意到,我大多只是替换String[]T[]添加<T>到方法签名中。

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;
}

编辑:

显然,要创建一个新数组,必须这样做:

public T[] createArray(Class<T> cls, int size) throws Exception   
{   
    @SuppressWarnings("unchecked") T[] array = (T[])Array.newInstance(cls, size);   
    return array;   
} 
于 2013-02-13T12:25:27.310 回答
1

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.

于 2013-02-13T12:32:36.057 回答