Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Constructor & container

  1. #1
    Junior Member
    Join Date
    Dec 2018
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Constructor & container

    I'd like to make working constructor creating a list filled with elements of the container.
    I have a program:
    import java.util.*;
     
    class Utils {
     
        public static <T extends Comparable<T>>
        void print(XList<T> list) {
            for (T elem : list) {
                System.out.println(elem);
            }
        }
    }
     
    public class XList<T extends Comparable<T>>
            implements Iterable<T> {
        ArrayList<Double> v = new ArrayList<Double>();
        Collection<T> list;
     
        public XList(Collection<T> list) {
            this.list = list;
        }
     
        public static void main(String[] args) {
            ArrayList<Double> v = new ArrayList<Double>();
            v.add(new Double(1.0));
            v.add(new Double(5.0));
            v.add(new Double(3.0));
            XList<Double> list = new XList<Double>(v);
            Utils.print(list);
        }
     
        @Override
        public Iterator<T> iterator() {
            return new Iterator<T>() {
                int i = 0;
     
                @Override
                public boolean hasNext() {
                    return i < list.size();
                }
     
                @Override
                public T next() {
                    if (hasNext()) {
                        return list.get(i++);       //here is a mistake...
                    }
                    return null;
                }
            };
        }
    }

    Is there anyone who could help me write it correctly?

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Constructor & container

    It is unclear to me what exactly you are trying to do. But here are some observations:

    1. Collection interfaces does not have a get method. So you need to either use the List implementation that backs your XList or convert the passed collection to an array (which is supported by Collection) and use that.

    2. In the following code
    public T next() {
       if (hasNext()) {
    	return list.get(i++); // here is a mistake...
       }
       return null;
    }

    Typically, you can just try and retrieve the value within a try/catch method and throw an exception if it doesn't exist (e.g. IndexOutOfBounds or something like that).

    3. The Double and other wrapper constructors are deprecated as of Java 9.
    So to create an instance of Double use Double.valueOf().


    Regards,
    Jim

  3. #3
    Junior Member
    Join Date
    Dec 2018
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Constructor & container

    Thanks for the answer.
    This is a part of the whole my exercise.
    The idea would be to use as much as possible existing elements from this exercise.
    I can do something like this:
    package TestXList;
     
    import java.util.ArrayList;
    import java.util.Collection;
     
    class XList<T extends Comparable<T>>{
     
        Collection<T> list;
     
        public XList(Collection<T> list) {
            this.list = list;
        }
    }

    and
    package TestXList;
     
    import java.util.ArrayList;
     
    public class TestXList {
        public static void main(String[] args) {
            ArrayList<Double> v = new ArrayList<Double>();
            v.add(new Double(1.0));
            v.add(new Double(5.0));
            v.add(new Double(3.0));
            ArrayList<Double> list = new ArrayList<Double>(v);
            list.stream().forEach(System.out::println);
        }
    }

    but I can't use the iterator or the class Utils...

Tags for this Thread