代码之家  ›  专栏  ›  技术社区  ›  Snowman

使用递归将数组转换为自定义LinkedList类

  •  0
  • Snowman  · 技术社区  · 15 年前

    我使用的自定义LinkedList类如下:

    public class LinkedList {
        // Get and Set methods are NOT necessary!
    
        private LinkedList next;    
        private final String word;
    
        public LinkedList(String word, LinkedList next) {
            this.word = word;
            this.next = next;
        }
    

    现在,我的任务是编写一个方法,它接受一个字符串数组,并将每个字符串对象覆盖到一个没有循环的LinkedList中,因此使用递归。没有循环怎么做?这对我来说是无法想象的。我从哪里开始?

    编辑: 让我澄清一下,我要编写的函数只接受一个参数,它是一个字符串数组,并返回一个LinkedList。

    8 回复  |  直到 15 年前
        1
  •  1
  •   Margus    15 年前

        public static LinkedList Of(String[] input) {
            if (input == null || input.length < 1)
                return null;
            return LinkedList.Of(input, input.length - 1);
        }
    
        public static LinkedList Of(String[] input, int i) {
            if (i > 0)
                return new LinkedList(input[i], LinkedList.Of(input, i - 1));
            return new LinkedList(input[i], null);
        }
    

        /**
         * Creates linked list from array input.
         * 
         * @param input
         *            data array
         * @return linked list with data
         */
        public static LinkedList Of(String[] input) {
            // Checks if array has elements.
            if (input == null || input.length < 1)
                return null;
            // Starts creating the array using overload 2.
            return LinkedList.Of(input, 0);
        }
    
        /**
         * Creates linked list from array input (overload 2).
         * 
         * @param input
         *            data array
         * @param i
         *            counter to remember at what element is current
         * @return linked list with data
         */
        public static LinkedList Of(String[] input, int i) {
            //Tests if counter is within array elements. 
            if (input.length - 1 > i)
                // Returns new element with (current element data, reference
                // to next element). Note that next element will be returned
                // by this same method (this is why it is recursive).
                return new LinkedList(input[i], LinkedList.Of(input, i + 1));
            //Last element. From here backtracking will begin. 
            return new LinkedList(input[i], null);
        }
    

        public String toString() {
            StringBuilder sb = new StringBuilder(this.word);
            LinkedList tmp = this;
            while (tmp.next != null) {
                sb.append(" > ");
                tmp = tmp.next;
                if (tmp.word != null)
                    sb.append(tmp.word);
            }
            return sb.toString();
        }
    

        String str = "Neque porro quisquam est qui dolorem ipsum quia "
                + "dolor sit amet, consectetur, adipisci velit...";
        LinkedList ll = LinkedList.Of(str.split("\\s+"));
        System.out.println(ll);
    
        2
  •  2
  •   ShasiSekar    14 年前
    enter code here
        public LinkedList arrayToLinkedList(String[] myArray)
    {
            String[] toConvert = myArray;
            List<String> toConvertList = (List) Arrays.asList(toConvert);
            LinkedList<String> convertedLinkedList = new LinkedList<String>(toConvertList); 
            return convertedLinkedList;
    }
    
        3
  •  1
  •   Mark Elliot    15 年前

    public LinkedList myfunction(String arr[]) {
        if(arr.empty == true)
            return void;
    
        //return the array minus the first element
        String shorterarray[] = substr(arr[],1);
    
        //recursively create the next element
        LinkedList myItem = new LinkedList(arr[0], myfunction(shorterarray[]));
    }
    

        4
  •  1
  •   Steven    15 年前

    public static void main(String[] args) {
        String[] data = new String[] { "1", "2", "3" };
    
        LinkedList head = build(data);
        while (head != null) {
            System.out.println(head.word);
            head = head.next;
        }
    }
    
    private static LinkedList build(String[] data) {
        if (data == null || data.length == 0) {
            return null;
        }
    
        LinkedList head = new LinkedList(data[0], null);
        build(head, data, 1);
        return head;
    }
    
    private static LinkedList build(LinkedList node, String[] data, int index) {
        if (index == data.length) {
            return node;
        }
    
        node.next = build(new LinkedList(data[index], null), data, ++index);
        return node;
    }
    
    private static class LinkedList {
        private final String word;
        private LinkedList next;    
    
        public LinkedList(String word, LinkedList next) {
            this.word = word;
            this.next = next;
        }
    }
    

        5
  •  1
  •   Laurence Gonsalves    15 年前

    private static LinkedList fromArray(String[] a, int offset) {
    

    LinkedList next offset

    public static LinkedList fromArray(String[] a) {
        return fromArray(a, 0);
    }
    

    offset >= a.length null return null

    a[offset]

        6
  •  0
  •   Dean J    15 年前

        7
  •  0
  •   zerodin    15 年前

    private LinkedList formlist(LinkedList list, String[] str, int length, int i) {
        if(i==length)
            return list;
        return formlist(new LinkedList (str[i],list),str,length,i+1);
    
    }
    
        8
  •  0
  •   Steven    15 年前

    private LinkedList build(String arr[]) {
        if(arr.length == 0)
            return null;
    
        //return the array minus the first element
        String shorterarray[] = Arrays.copyOfRange(arr, 1, arr.length);
    
        //recursively create the next element
        return new LinkedList(arr[0], build(shorterarray));
    }