Java在CSV中从100行中随机选择10行



我正在处理Java程序,并且我有一个带有100行的data.csv文件。我想随机选择10行。数据看起来像这样:

    T1  T2  T3  T4  T5
    1   1.0  1   0   1
    1   1.0  0   0   1
    0   0.0  1   1   0

我已经使用以下代码在CSV文件中读取:

public static void main(String[] args) throws IOException {
    try
    {
        Scanner readIn = new Scanner (new File ("data.csv") );
        while ( readIn.hasNext() )
        {               
            line = readIn.nextLine();
            str = line.split(",",-1);        
        }
        readIn.close();     
    }
    catch (ArrayIndexOutOfBoundsException ob)
    {
        System.out.println("File not found..." );
    } 
    catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    }
}   

更新带有重复号码的代码:

    int j=0;
    Map<Integer,Integer> numberMap=new HashMap<Integer,Integer>();
    SecureRandom random = new SecureRandom(); 
    while(j!=10)
    {
        int row = random.nextInt(list.size()); 
        if(!numberMap.containsKey(Integer.valueOf(row)))
        {
            numberMap.put(Integer.valueOf(row), Integer.valueOf(row));
            System.out.println("Row "+row+"="+list.get(row));
            j++;
        }
    }

最新更新