PDA

Click to See Complete Forum and Search --> : Need some help


System_Error
Dec 18th, 2004, 07:10 AM
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:

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.

System_Error
Dec 18th, 2004, 09:42 AM
what about this, is it a better way of doing it?


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();
}

Dillinger4
Dec 18th, 2004, 10:54 AM
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.

Dillinger4
Dec 18th, 2004, 11:04 AM
The code does compile fine though and the output seems to be what was intended.
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);
}
}
}

System_Error
Dec 18th, 2004, 11:32 AM
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?

Dillinger4
Dec 18th, 2004, 11:44 AM
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.

Dillinger4
Dec 18th, 2004, 11:52 AM
Creating an alias works also.

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());
}
}
}

System_Error
Dec 18th, 2004, 01:03 PM
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?

Dillinger4
Dec 18th, 2004, 02:48 PM
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.

System_Error
Dec 18th, 2004, 03:55 PM
Ok. I see. Thank you.