Java知识回顾:怎么用JAVA代码实现从数组随机获取一个元素
int index = (int) (Math.random() * temp.length);//先生成一个随机的数组下标
system.out.println(temp[index]);
测试代码如下:
1 @Test
2 public void testUntitled() throws Exception {
3 driver.get(baseUrl);
4 File f=new File("C:\\filedir\\");
5 File list[] = f.listFiles();
6 String temp[ ] = new String[list.length];
7 for(int i=0;i<list.length;i++){
8 if(list[i].isFile()){
9 temp[i] = list[i].getName();
10 }
11 }//循环遍历取出文件列表的文件名
12 for(int i=0;i<temp.length;i++){
13 System.out.println(temp[i]);
14 }//打印出取到的文件名
15 int index = (int) (Math.random() * temp.length);//先生成一个随机的数组下标
16 WebElement adFileUpload=driver.findElement(By.name("file"));
17 String filePath="C:\\filedir\\"+temp[index];
18 adFileUpload.sendKeys(filePath);
19 Thread.sleep(2000);
20 }