Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2015
    Posts
    17
    Rep Power
    0

    Question How to write a program

    Hello!

    I'm new to Java and I'm learning collections before my test which would be the 6th of November.

    I took some exercises on it on Java Collections |

    Write a program to iterate through an ArrayList.

    I tried:

    Java Code:
    package exercices;
    
    public class IterateArrayList {
    	e = new arrayList;
    	e.add(1);
    	e.add(2);
    	e.add(3);
    	
    	g = new arrayList;
    	
    	while (e.hasNext){
    		g.iterator(e);
    	}
    }
    I know this doesn't work but what did I missed to make it?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: How to write a program

    A few remarks:

    0) e isn't defined anywhere; the compiler doesn't like that;
    1) all code you need or want to execute should be in a method; your code is at the class level and the compiler doesn't like that;
    2) it'a an ArrayList, not an arrayList; the compiler complains about it;
    3) an ArrayList should be generically typed; here: ArrayList<Integer>;
    4) an ArrayList doesn't have a hasNext member (neither a method, nor any other identifier); the compiler doesn't like that;
    5) what is the method iterator(e) supposed to be? You made that up yourself did you?
    6) g in line 11 isn't defined anywhere; again, the compiler doesn't like that.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Join Date
    Nov 2015
    Posts
    17
    Rep Power
    0

    Question Re: How to write a program

    Thank you for your advises and your time!

    Okay, I understand 0., 1. and 2.
    I didn't understood the third point: an ArrayList should be generically typed; here: ArrayList<Integer>;
    4) I didn't knew that an ArrayList didn't had an hasNext member (neither a method, nor any other identifier);
    5) The iterator is produced by the interface Collection, it gives an iterator of the collection (from my lecture). I made that up myself, yes...
    If I can't use iterator, how should I do, do you have an hint?

    This is my updated answer:

    Java Code:
    package exercices;
    import java.util.*;
    
    class Iter(List g){
    	List e = new ArrayList;
    	e.add(1);
    	e.add(2);
    	e.add(3);
    	
    
    	
    	while (e.hasNext){
    		g.iterator(e);
    	}
    }
    
    public class IterateArrayList {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    	List g = new ArrayList;
    
           Iter(g);
    	}
    
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to write a program

    how should I do, do you have an hint?
    Yes, read the API doc for the ArrayList class found here: Java Platform SE 8
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to write a program

    Usually when one has a test it is on topics covered in class. And I would presume the instructor gave assignments related
    to those topics. Did you not understand them? Do you have notes, slides, or a text to consult?

    Regards,
    Jim
    Last edited by jim829; 11-30-2015 at 09:05 PM. Reason: first sentence - terrible grammar
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Join Date
    Nov 2015
    Posts
    17
    Rep Power
    0

    Default Re: How to write a program

    Quote Originally Posted by Norm View Post
    Yes, read the API doc for the ArrayList class found here: Java Platform SE 8
    Hello, thanks for the link,

    Sorry, I'm a beginner, I don't understand the link you sent, how to use it...

  7. #7
    Join Date
    Nov 2015
    Posts
    17
    Rep Power
    0

    Default Re: How to write a program

    Quote Originally Posted by jim829 View Post
    Usually when one has a test of topics covered in class. And I would presume the instructor gave assignments related
    to those topics. Did you not understand them? Do you have notes, slides, or a text to consult?

    Regards,
    Jim
    We had one exercise on it, and I didn't understood it. That's why I try to do some more exercises to understand the collections. My textbook is very light...

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: How to write a program

    Quote Originally Posted by AntoineCompagnie View Post
    Hello, thanks for the link,

    Sorry, I'm a beginner, I don't understand the link you sent, how to use it...
    In the lower left panel you'll find a sorted list of all available classes; e.g click on the ArrayList entry; on the panel on the right you'll find everything you wanted to know about the class or interface.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to write a program

    Okay, I will explain it in more layman's terms. First you must grab an iterator (read the API to see how to get an iterator). Then you check hasNext(). It returns true for more values and false if you have exhausted the list. The next() method returns the value.

    Now you just have to figure out how to put it together in a loop.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to write a program

    Looking at that code that's been posted here I would say there's a more fundamental problem. You don't seem to know what the basic structure of a Java class is, and where you can put code.
    I would suggest re-reading any notes you have on that before worrying too much about iterators.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 19
    Last Post: 07-17-2015, 04:40 PM
  2. Replies: 7
    Last Post: 11-30-2011, 07:58 PM
  3. Write a program..??
    By QSilver in forum New To Java
    Replies: 7
    Last Post: 02-21-2011, 03:20 PM
  4. how can write program for gui
    By pothireddy in forum Advanced Java
    Replies: 9
    Last Post: 11-04-2010, 07:17 AM
  5. Trying to write my own program : /
    By jdicerch in forum New To Java
    Replies: 50
    Last Post: 09-26-2010, 02:44 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •