-
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.
-
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();
}
-
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.
-
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);
}
}
}
-
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?
-
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.
-
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());
}
}
}
-
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?
-
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.
-
Re: Need some help