List arr = new ArrayList();
        arr.add("fdsf");
        arr.add("asdf");
        arr.add("abc");
        arr.add("fdac");
        arr.add("asdf");
        arr.add("abc");
怎么把这里边的“abc”删除?想了半天,我是菜鸟不行不会

解决方案 »

  1.   

    List ls = new ArrayList();
    ls.add("abc");
    arr.removeAll(ls);
      

  2.   

    要做删除不要用List,list查询速度快
    如果做增删除用linklst或map比较好
      

  3.   


      List arr = new ArrayList();
      arr.add("fdsf");
      arr.add("asdf");
      arr.add("abc");
      arr.add("fdac");
      arr.add("asdf");
      arr.add("abc");
      ArrayList a=new ArrayList();//创建一个新集合
      a.add("abc");//添加要删除的
      al.removeAll(a);//删除所以a里包含的
      

  4.   

    哥们我说是我里边已经加上了
    arr.add("fdsf");
      arr.add("asdf");
      arr.add("abc");
      arr.add("fdac");
      arr.add("asdf");
      arr.add("abc");
    然后怎么删掉里边的“abc”,好像是要用到遍历
      

  5.   


    /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    List<String> arr = new ArrayList<String>();
      arr.add("fdsf");
      arr.add("asdf");
      arr.add("abc");
      arr.add("fdac");
      arr.add("asdf");
      arr.add("abc");
      
      for(String str : Test.removeList(arr, "abc")) {
      
      System.out.println(str);
      }
    }

    public static List<String> removeList(List<String> arr, String removeStr) {

    List<String> list = new ArrayList<String>();

    for(String str : arr) {

    if(!str.equals(removeStr) || str != removeStr) {
      
    list.add(str);

     }

    }

    return list;

    }
      

  6.   

    还有一个题,看能不能写下代码,写了半天错了找出d盘下所有的 .java 文件,拷贝至 c:\jad 目录下,并将所有文件的类型由.java 修
          改为.jad
    public class CopyFile {
    public static void main(String[] args) throws IOException {
    File fromPath = new File("D:");
    File toPath = new File("c:\\jad");
    checkFiles(fromPath, toPath);
    } private static void checkFiles(File fromPath, File toPath)
    throws IOException {
    if (!fromPath.exists()) {// 测试此抽象路径名表示的文件或目录是否存在
    throw new RuntimeException("文件不存在,请重新输入!");
    } else if (fromPath.isFile() && fromPath.getName().endsWith(".java")) {
    copyFiles(fromPath, toPath);
    } else {
    File[] files = fromPath.listFiles();
    for (File file : files) {// 对数组进行遍历
    if (file.isDirectory()) {
    checkFiles(file, toPath);
    } else if (file.getName().endsWith(".java")) {
    copyFiles(file, toPath);
    }
    }
    }
    }
    private static void copyFiles(File cFile, File toDir) throws IOException {//拷贝不了怎么回事
      String fileName = cFile.getName();
      FileInputStream fis = new FileInputStream(cFile);
      FileOutputStream fos = new FileOutputStream(new File(toDir, fileName));
      try {
        byte[] b = new byte[8192]; 
        int ch = 0;
        while (-1 != (ch = fis.read(b))) {
          fos.write(b, 0, ch);
        }
      } finally {
        fos.close();
        fis.close();
      }  
    }
    }
      

  7.   


    package a;import java.util.ArrayList;
    import java.util.List;public class Test1 { /**
     * @param args
     */
    public static void main(String[] args) {
    List arr = new ArrayList();
    arr.add("fdsf");
    arr.add("asdf");
    arr.add("abc");
    arr.add("fdac");
    arr.add("asdf");
    arr.add("abc");
    System.out.println("删除前:") ;
    for (int i = 0; i< arr.size(); i++){
    System.out.print(arr.get(i) + " ") ;
    }
    System.out.println() ;
    for (int i = arr.size()-1 ; i>=0; i--){
    if ("abc".equals(arr.get(i))) {
    arr.remove(i) ;
    }
    }
    System.out.println("删除后:") ;
    for (int i = 0; i< arr.size(); i++){
    System.out.print(arr.get(i) + " ") ;
    }
    System.out.println() ; }}
    删除前:
    fdsf asdf abc fdac asdf abc 
    删除后:
    fdsf asdf fdac asdf 
      

  8.   


    package a;import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;public class CopyFile {
    public static void main(String[] args) throws IOException {
    File fromPath = new File("D:");
    File toPath = new File("c:\\jad");
    checkFiles(fromPath, toPath);
    }private static void checkFiles(File fromPath, File toPath)
    throws IOException {
    if (!fromPath.exists()) {// 测试此抽象路径名表示的文件或目录是否存在
    throw new RuntimeException("文件不存在,请重新输入!");
    } else if (fromPath.isFile() && fromPath.getName().endsWith(".java")) {
    copyFiles(fromPath, toPath);
    } else {
    File[] files = fromPath.listFiles();
    for (File file : files) {// 对数组进行遍历
    if (file.isDirectory()) {
    checkFiles(file, toPath);
    } else if (file.getName().endsWith(".java")) {
    copyFiles(file, toPath);
    }
    }
    }
    }
    private static void copyFiles(File cFile, File toDir) throws IOException {//拷贝不了怎么回事
    String fileName = cFile.getName();
    FileInputStream fis = null;
    FileOutputStream fos = null ;
    try {
                    //放到 try中新建
    fis = new FileInputStream(cFile) ;
    fos = new FileOutputStream(new File(toDir, fileName));
    byte[] b = new byte[8192];  
    int ch = 0;
    while (-1 != (ch = fis.read(b))) {
    fos.write(b, 0, ch);
    }
    } finally {
    fos.close();
    fis.close();
    }   
    }
    }这个代码可以 啊
      

  9.   

    Exception in thread "main" java.lang.NullPointerException
    at com.heima.changjaije.Five.CopyFile.checkFiles(CopyFile.java:34)
    at com.heima.changjaije.Five.CopyFile.checkFiles(CopyFile.java:36)
    at com.heima.changjaije.Five.CopyFile.checkFiles(CopyFile.java:36)
    at com.heima.changjaije.Five.CopyFile.main(CopyFile.java:23)提示这个错误,大侠指点那.怎么回事?
      

  10.   

    你在C:先建立目录 c:\jad\hello.java
      

  11.   

    at com.heima.changjaije.Five.CopyFile.checkFiles(CopyFile.java:34)是哪一行 ?FileInputStream fis = new FileInputStream(cFile) ; ??你看看你文件是否存在 ?我在我机器上都是可以的 ,需建立 C:\\jda ,同时你的 D: 需要有.java文件 (可以写一个判断,如果没有也没关系 ,你看看下面的代码)package a;import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;public class CopyFile {
    public static void main(String[] args) throws IOException {
    File fromPath = new File("D:");
    File toPath = new File("c:\\jad");
    checkFiles(fromPath, toPath);
    } private static void checkFiles(File fromPath, File toPath)
    throws IOException {
    if (!fromPath.exists()) {// 测试此抽象路径名表示的文件或目录是否存在
    throw new RuntimeException("文件不存在,请重新输入!");
    } else if (fromPath.isFile() && fromPath.getName().endsWith(".java")) {
    copyFiles(fromPath, toPath);
    } else {
    File[] files = fromPath.listFiles();
    for (File file : files) {// 对数组进行遍历
    if (file.isDirectory()) {
    checkFiles(file, toPath);
    } else if (file.getName().endsWith(".java")) {
    copyFiles(file, toPath);
    }
    }
    }
    } private static void copyFiles(File cFile, File toDir) throws IOException {// 拷贝不了怎么回事
    String fileName = cFile.getName();
    FileInputStream fis = null;
    FileOutputStream fos = null;
    if (toDir != null) { //判断一下 ,如果没.java文件,则不做任何操作
    try {
    fis = new FileInputStream(cFile);
    fos = new FileOutputStream(new File(toDir, fileName));
    byte[] b = new byte[8192];
    int ch = 0;
    while (-1 != (ch = fis.read(b))) {
    fos.write(b, 0, ch);
    }
    } finally {
    fos.close();
    fis.close();
    }
    }
    }
    }用这段代码试试
      

  12.   

     checkFiles(fromPath, toPath);
    这行
      

  13.   


    private static void checkFiles(File fromPath, File toPath)
                throws IOException {
            if (fromPath != null && !fromPath.exists()) {// 测试此抽象路径名表示的文件或目录是否存在
                throw new RuntimeException("文件不存在,请重新输入!");
            } else if (fromPath.isFile() && fromPath.getName().endsWith(".java")) {
                copyFiles(fromPath, toPath);
            } else {
                File[] files = fromPath.listFiles();
                for (File file : files) {// 对数组进行遍历
                    if (file.isDirectory()) {
                        checkFiles(file, toPath);
                    } else if (file.getName().endsWith(".java")) {
                        copyFiles(file, toPath);
                    }
                }
            }
        }
      

  14.   

    获得里面的对象,然后对比一下,把想删的直接remove