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 6 of 6

Thread: Doubt Regarding Printing an Array List

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Doubt Regarding Printing an Array List

    [SIZE="3"]import java.util.ArrayList;
    class books
    {
    	String authour;
    	String title;
     
    }
     class booktest {
    	public static void main(String[] args)
    	{
    		int x=0;
    		ArrayList<books> myList =new ArrayList<books>();
    		 books a=new books();
    		 myList.add(a);
    		 a.authour="welcome";
    		 a.title="home";
    		 books b=new books();
    		 myList.add(b);
    		 b.authour="welc";
    		 b.title="ho";
    		 while(x<2)
    			 {
    			 	System.out.println(myList.add );//confused how to display the following thing
    			 	System.out.println();// how to display the following thing
    			 x=x+1;
    			 }
    			 }
     
    }[/SIZE]
    My query is how can we able to print the following program.

    I used System.out.println(books[x].authour)//i guess it is wrong and it is not compiled too


  2. #2
    Junior Member planmaster's Avatar
    Join Date
    Apr 2010
    Location
    Yerevan
    Posts
    7
    Thanks
    5
    Thanked 1 Time in 1 Post

    Post Re: Doubt Regarding Printing an Array List

    I tried 15 minutes and get something like this
    import java.util.ArrayList;

    class books
    {
    String authour;
    String title;

    public void print()
    {
    System.out.println("Author = " + this.authour + ". title = " + this.title);
    System.out.println();
    }

    }
    class booktest {
    public static void main(String[] args)
    {
    int x=0;
    ArrayList<books> myList =new ArrayList<books>();
    books a=new books();
    myList.add(a);
    a.authour="welcome";
    a.title="home";
    books b=new books();
    myList.add(b);
    b.authour="welc";
    b.title="ho";
    while(x<2)
    {
    books value = myList.get(x);
    value.print();
    x=x+1;
    }
    }

    }
    If this isn't that u espect please contact with me, maybe I can help u

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Doubt Regarding Printing an Array List

    I appreciate your work.You have invoked method print and done it i guess.Thanks


    Well is it possible to directly provide arraylist in while statement
    while(x<2)

    {

    System.out.println(myList.add )//i mean here without invoking method print
    System.out.println();// how to display the following thing
    x=x+1;
    }

  4. #4
    Junior Member planmaster's Avatar
    Join Date
    Apr 2010
    Location
    Yerevan
    Posts
    7
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Doubt Regarding Printing an Array List

    I don't know but I tried this method but it didn't work
    I used the myList.get(x) method but printed something like address.
    My advice write code in the classes how u can. And declare private everything u can.
    Maybe there is another solution of your problem but I don't know I am beginner

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Doubt Regarding Printing an Array List

    Are you just trying to print the contents of the ArrayList? You can use a for loop to do so:
    for ( int i = 0; i < myList.size(); i++ ){
        System.out.println(myList.get(i).author + " " myList.get(i).title);
    }

    An alternative way to do this is to 'print the object', overriding the toString() method of you books class

    class books
    {
    	String authour;
    	String title;
    	@Override public String toString(){
                return author + " " + title;
            }
    }
    ....
    for ( int i = 0; i < myList.size(); i++ ){
        System.out.println(myList.get(i));
    }

  6. The Following User Says Thank You to copeg For This Useful Post:

    planmaster (April 19th, 2010)

  7. #6
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: Doubt Regarding Printing an Array List

    I would have done something along the line of:
    import java.util.ArrayList;
     
    class Book
    {
    	int bookid;
    	String author;
    	String title;	
    }
     
    class BookReader {
    	public static void main(String[] args)
    	{
    		Hashtable<String, Book> booklist = new Hashtable<int, Book>(); //now you can search with just the bookid
     
    		Book a = new Book();
    		a.bookid = 1;
    		a.author = "welcome";
    		a.title = "home";
    		booklist.putt(a.bookid, a);
     
    		Book b = new Book();
    		b.bookid = 2;
    		b.author = "welc";
    		b.title = "ho";
    		booklist.put(b.bookid, b);
     
    		for(Book book : booklist) //now you have the object directly available and can easily work with the object :)
    		{
    			String booktitle = (String)book.title;
    			String bookauthor = (String)book.author;
    			System.out.println("The book "+booktitle+" was written by: "+bookauthor);
    		}
    	}
    }

    ps. im at work right now so dont mind the possible syntax errors.
    Last edited by Bryan; April 20th, 2010 at 03:34 AM.

Similar Threads

  1. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM
  2. Doubt regarding LIST
    By puneetsr in forum Collections and Generics
    Replies: 1
    Last Post: February 23rd, 2010, 04:19 PM
  3. doubt in hibernate
    By jyothishey in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 10th, 2010, 11:43 PM
  4. Which javadoc tag is used to denote a comment for a method parameter?
    By TARUN JORA in forum Java Theory & Questions
    Replies: 2
    Last Post: May 15th, 2009, 04:30 PM
  5. Replies: 1
    Last Post: November 22nd, 2008, 01:32 PM