设计一个程序,包含一个无参方法,此方法返回List中的奇数元素,这些奇数元素存在新List中。
运行后提示用户输入list:
例如:输入List1:as,sd,df,fg,gh
     输出List2: as,df,gh 

解决方案 »

  1.   

    貌似借助ArrayList类和System.in.read()方法就足够了....
      

  2.   

    for(i=0;i<链表.length;i=i+2)
    {
    System.out.println(链表[i]);
    }
      

  3.   

    List<String> strlist=new ArrayList<String>(); //LinkedList?
    List<String> newlist=new ArrayList<String>();……
    int i=1;
    for(String str:strlist){
      if(++i%2==0){
         newlist.add(str);
      }
    }
    嘿嘿 这样不用Iterator了
      

  4.   


    class Test {    static List<String> listold = new ArrayList<String>();    static List<String> listnew = new ArrayList<String>();    Test() {
            BufferedReader d = new BufferedReader(new InputStreamReader(System.in));
            Scanner scanner = new Scanner(d);
            try {
                String s = d.readLine();
                String[] s2 = s.split(",");
                listold = Arrays.asList(s2);
                // System.out.println(d.readLine());
            } catch (IOException e) {
                // TODO 自動生成された catch ブロック
                e.printStackTrace();
            }
        }    public static void main(String[] args) {
            listnew = new Test().test();
            System.out.println(listnew);
        }    List<String> test() {
            for (int i = 0; i < listold.size(); i++) {
                if (i % 2 == 0) {
                    listnew.add(listold.get(i));
                }
            }
            return listnew;
        }}
      

  5.   

    稍微改了下
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Scanner;class Test1 {    static List<String> listold = new ArrayList<String>();    static List<String> listnew = new ArrayList<String>();    Test1() {
            BufferedReader d = new BufferedReader(new InputStreamReader(System.in));
            Scanner scanner = new Scanner(d);
            System.out.println("输入List1:");
            try {
                String s = d.readLine();
                String[] s2 = s.split(",");
                listold = Arrays.asList(s2);
            } catch (IOException e) {
                 e.printStackTrace();
            }
        }    public static void main(String[] args) {
            listnew = new Test1().test();
            System.out.print("输出List2:");
            for (int i = 0; i < listnew.size(); i++) {
                if (i != (listnew.size() - 1)) {
                    System.out.print(listnew.get(i) + ",");
                } else {
                    System.out.print(listnew.get(i));
                }
            }
        }    List<String> test() {
            for (int i = 0; i < listold.size(); i++) {
                if (i % 2 == 0) {
                    listnew.add(listold.get(i));
                }
            }
            return listnew;
        }}
      

  6.   

    // TODO 自動生成された catch ブロック????