Help with displaying contents of ArrayList


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Help with displaying contents of ArrayList

Hybrid View

  1. #1
    Join Date
    Oct 2006
    Posts
    2

    Help with displaying contents of ArrayList

    I am trying to put an object w into an ArrayList, then display the contents of w, which is in the ArrayList. Here are the two classes I am using:

    Code:
    import java.util.*;
    public class App {
     
     
    	
    	public static void main(String[] args) {
    		
    	ArrayList test = new ArrayList();
    		
    		Worker w = new Worker();
     
    		w.setAttributes();
    		
    		w.testOutput();
    		
    		test.add(w);
    		
    		
    		
    	
    	}
     
    	
    	
    }







    Code:
    import java.util.*;
    public class Worker {
     
    	String name;
    	String name2;
    	int floor;
    	int ceiling;
    	
    	
    public Worker(){}
    public Worker(String name, String name2, int floor, int ceiling)
    {
     
    }
    	
    public void setAttributes()
    {
    	name = "Todd";
    	name2 = "McCree";
    	floor = 1;
    	ceiling = 8;
    }
     
    public void testOutput()
    {
    	System.out.println(name + "\n" + name2 + "\n" + floor + "\n" + ceiling);	
    }
     
     
     
    }
    I have the method testOutput() just to verify that my attributes are being set accordingly. It's driving me crazy not being able to figure out how to print out the contents of the ArrayList. Any help is greatly appreciated. Thanks to all!

  2. #2
    Join Date
    Sep 2006
    Posts
    68
    ArrayList test = new ArrayList();
    Iterator it = test.iterator();
    while(it.hasNext()) {
    Worker w = (Worker)it.next();
    w.testOutput();
    }

  3. #3
    Join Date
    Oct 2006
    Posts
    2
    I tried this, but it didn't display anything. What am I doing wrong?

    Code:
    import java.util.*;
    public class App {
    
    
    	
    	public static void main(String[] args) {
    		
    	
    		ArrayList test = new ArrayList();
    		Iterator it = test.iterator();
    		while(it.hasNext()) {
    		Worker w = (Worker)it.next();
    		w.testOutput();
    		test.add(w);
    		}
    
    		
    		
    	
    		}

  4. #4
    Join Date
    Sep 2006
    Posts
    68
    You are iterating over the empty collection. Yot have to add some objects after
    ArrayList test = new ArrayList(); string.

    ArrayList test = new ArrayList();
    Worker wk = new Worker();
    wk.setAttributes();
    test.add(wk);

    Iterator it = test.iterator();
    while(it.hasNext()) {
    Worker w = (Worker)it.next();
    w.testOutput();
    }

Similar Threads

  1. Replies: 1
    Last Post: 12-24-2005, 06:12 AM
  2. Replies: 10
    Last Post: 05-15-2005, 06:14 AM
  3. C# Debugging - Displaying Array Contents
    By Keith Wedinger in forum .NET
    Replies: 0
    Last Post: 02-13-2001, 01:08 PM
  4. Replies: 2
    Last Post: 05-31-2000, 10:11 AM
  5. Replies: 0
    Last Post: 04-16-2000, 02:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center