Results 1 to 10 of 10

Thread: Need some help

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Resolved Need some help

    I have something simple here I cannot figure out, and I don't know why. I'm trying to create a method that gets a list and then a method that prints that same list. Here is my code:
    Code:
    import java.util.List;
    import java.util.LinkedList;
    import java.util.Iterator;
    
    public class TestGenerics2
    {
    	public List<String> getList()
    	{
    		List<String> list = new LinkedList<String>();
    		list.add("One");
    		list.add("Two");
    		list.add("Three");
    		
    		return list;
    	}
    	
    	public String showList(List<String> list)
    	{
    		List list = this.getList();
    		
    		for(Iterator i = list2.iterator; i.hasNext();)
    		{
    			System.out.println(i.next());
    		}
    	}
    	
    }
    I know this could have been done in one method easily, but I wanted the second one.
    Last edited by System_Error; Dec 18th, 2004 at 04:55 PM.

  2. #2

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Need some help

    what about this, is it a better way of doing it?

    Code:
    private static List<Integer> ints = new ArrayList<Integer>();
    	
    	public static void fillList(List<Integer> list)
    	{
    		for (Integer i : list)
    		{
    			ints.add(i);
    		}
    	}
    	
    	public static void printList()
    	{
    		for (Integer i : ints)
    		{
    			System.out.println(i);
    		}
    	}
    	
    	public static void main(String[] args)
    	{
    		List<Integer> myInts = new ArrayList<Integer>();
    		myInts.add(1);
    		myInts.add(2);
    		myInts.add(3);
    		myInts.add(4);
    		fillList(myInts);
    		printList();
    	}

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Need some help

    The second block of code that you posted seems quite confusing. It seems you are passing the ArrayList myInts out of the method and then to the fillList method but then within the fillList method you are referencing an entirely different ArrayList ints which is declared as a static class mamber.

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Need some help

    The code does compile fine though and the output seems to be what was intended.
    Code:
    import java.util.*;
    
     public class X{
      public static void main(String[] args){
         List<Integer> myInts = new ArrayList<Integer>();
         myInts.add(1);
         myInts.add(2);
         myInts.add(3);
         myInts.add(4);
         fillList(myInts);
         printList();
       }
    
    
      private static List<Integer> ints = new ArrayList<Integer>();
       public static void fillList(List<Integer> list){
        for(Integer i : list){
         ints.add(i);
        }
       }
    	
       public static void printList(){
        for(Integer i : ints){
         System.out.println(i);
        }
       }
      }

  5. #5

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Need some help

    Well, I got the printList() method out of a book and they used the private static ... like the one example I posted. I really don't understand it though. Actually I don't know how it compiles either. So if you understand it could you explain it?

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Need some help

    From the way i see it an ArrayList myInts is created in the main method. It's filled with values then passed to the fillList method which simply takes the contents of the List passed as an arguement and transfers those to the ints ArrayList which is a class member.

  7. #7
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Need some help

    Creating an alias works also.
    Code:
    import java.util.*;
    
     public class X{
      public static void main(String[] args){
         ArrayList<Integer> myInts = new ArrayList<Integer>();
         myInts.add(1);
         myInts.add(2);
         myInts.add(3);
         myInts.add(4);
         fillList(myInts);
         printList();
       }
      
       private static ArrayList<Integer> ints = new ArrayList<Integer>();
        public static void fillList(ArrayList<Integer> list){
    	ints = list;
        }
    	
       public static void printList(){
         for(Iterator i = ints.iterator(); i.hasNext();){
           System.out.println(i.next());
        }
       }
      }

  8. #8

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Need some help

    Thanks Dilenger. That made it much more readable and understanble. But I still don't see how in the old example, the ints were cast to list. Also, that one member was static, so how could it be changed?

  9. #9
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Need some help

    The ArrayList member ints has to remain static or you are accessing a non-static member from static contexts ie. the filllList() and printList() methods.
    If you change ArrayList ints to non-static and the method signatures of fillList() and printList() to non-static you still run into the same problem trying to access a non-static member from the static main context.

  10. #10

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Need some help

    Ok. I see. Thank you.

Posting Permissions

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



Click Here to Expand Forum to Full Width