日期:2014-05-20  浏览次数:20804 次

list集合里怎么删除想删的字符串?菜鸟不会,请高手解答
List arr = new ArrayList();
  arr.add("fdsf");
  arr.add("asdf");
  arr.add("abc");
  arr.add("fdac");
  arr.add("asdf");
  arr.add("abc");
怎么把这里边的“abc”删除?想了半天,我是菜鸟不行不会

------解决方案--------------------
Java code

  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里包含的

------解决方案--------------------
Java code

/**
     * @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;
        
    }

------解决方案--------------------
Java code

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() ;

    }

}

------解决方案--------------------
你在C:先建立目录 c:\jad\hello.java
------解决方案--------------------
at com.heima.changjaije.Five.CopyFile.checkFiles(CopyFile.java:34)

是哪一行 ?FileInputStream fis = new FileInputStream(cFile) ; ??

你看看你文件是否存在 ?

我在我机器上都是可以的 ,需建立 C:\\jda ,同时你的 D: 需要有.java文件 (可以写一个判断,如果没有也没关系 ,你看看下面的代码)

Java code
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();
            }
        }
    }
}