ผลต่างระหว่างรุ่นของ "01204212/recur"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
แถว 2: แถว 2:
  
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
package adt_ex_w6_recur;
 
 
 
import java.util.Iterator;
 
import java.util.Iterator;
 
import java.util.LinkedList;
 
import java.util.LinkedList;

รุ่นแก้ไขเมื่อ 07:19, 15 กันยายน 2559

countIf

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

public class Main {

    static private int countIfLoop(LinkedList<Integer> list, int target) {
        ListIterator<Integer> ii = list.listIterator();
        int count = 0;
        while(ii.hasNext()) {
            if(ii.next() == target) {
                count++;
            }
        }
        return count;
    }
    
    static private int countIf(LinkedList<Integer> list, int target) {
        ListIterator<Integer> ii = list.listIterator();
        return countIf0(ii,target);
    }
    
    static private int countIf0(ListIterator<Integer> ii, int target) {
        // write your code here
        return 0;
    }

    static private int findSize0(ListIterator<Integer> ii) {
        if(! ii.hasNext()) {
            return 0;
        } else {
            ii.next();
            int k = findSize0(ii);
            return k + 1;
        }
    }

    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<Integer>();
        
        list.add(10); list.add(200); list.add(15);
        list.add(11); list.add(200); list.add(15);
        list.add(12); list.add(201); list.add(15);
        list.add(13); list.add(200); list.add(15);

        System.out.println(countIf(list,10));
        System.out.println(countIf(list,15));
        System.out.println(countIf(list,20));
        System.out.println(countIf(list,200));
    }

    private static void printList(LinkedList<Integer> list) {
        Iterator<Integer> i = list.iterator();
        while(i.hasNext()) {
            System.out.print("" + i.next() + " ");
        }
        System.out.println();
    }
}