passing a link list to a method
hi i have two class files first is fileIO.java and the other is the main!
i want to create a linklist in main and pass it to a method in fileIO.java. the method is static..
fileRead(File dataFile, List myList){}
doesn't seem to work.. can somebody give me a pointer?
[345 byte] By [
mark_22a] at [2007-9-29 12:27:22]

http://java.sun.com/docs/books/tutorial/index.html
sorry it was a stupid question, its 1am here.. the list is an object :)should of switched on.. thanks for posting an address though to such a vague page :)
How much info does it take to answer a question?
Do you know how to formulate data in a linked list?
Do you know how to link 2 java class files?
Do you understand OOP design?
Do you know the way to San Jose? ...Wo oh oh oh, Can't wait to get back to San Jose, Wo oh oh oh ...
>
> How much info does it take to answer a question?
>
> Do you know how to formulate data in a linked list?
> Do you know how to link 2 java class files?
> Do you understand OOP design?
YES
YES
YES
i've got a fileIO class and a static method to read objects into a object called CAR, how do i get the link list passed so i can use it?
the link list is in the main file, (i'm new to link list BTW) and the fileIO class is in a FileIO.java file (duh :)) my method/constructor for the read objects is
public static void getCarData(File dataFile, Object List)
i call it like!
LinkedList carList= new LinkedList();
FileIO.getCarData(*my data file*, carList)
can u tell me what i'm doing wrong?
thanks!
> >
> > How much info does it take to answer a question?
> >
> > Do you know how to formulate data in a linked list?
> > Do you know how to link 2 java class files?
> > Do you understand OOP design?
>
> YES
> YES
> YES
>
> i've got a fileIO class and a static method to read
> objects into a object called CAR, how do i get the
> link list passed so i can use it?
>
> the link list is in the main file, (i'm new to link
> list BTW) and the fileIO class is in a FileIO.java
> file (duh :)) my method/constructor for the read
> objects is
> > public static void getCarData(File dataFile, Object
> List)
>
> i call it like!
>
> > LinkedList carList= new LinkedList();
> FileIO.getCarData(*my data file*, carList)
>
>
> can u tell me what i'm doing wrong?
> thanks!
>
you should declare it as
public static void getCarData(File myfile, List carlist);
> > > > LinkedList carList= new LinkedList();
> > FileIO.getCarData(*my data file*, carList)
> >
> >
> > can u tell me what i'm doing wrong?
> > thanks!
> >
> you should declare it as
> public static void getCarData(File myfile, List
> carlist);
>
if i declare it like that, i still can't add to the list
FileInputStream fis = new FileInputStream(dataFile);
ObjectInputStream ois = new ObjectInputStream(fis);
Car c = (Car)ois.readObject();
carList.addLast(c);
ERROR: --> testfile.java:63: cannot resolve symbol
This worked for me:
import java.io.*;
import java.util.*;
public class ListAdder
{
public static void main(String [] args)
{
try
{
if (args.length > 0)
{
List values = new ArrayList();
ListAdder.readValues(args[0], values);
System.out.println(values);
}
}
catch (Exception e)
{
e.printStackTrace(System.err);
}
}
public static void readValues(final String fileName, List values)
throws FileNotFoundException, IOException
{
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String line = "";
while ((line = br.readLine()) != null)
values.add(line);
br.close();
}
}
It just reads strings from a file, but it did modify the List. MOD
thanks duffymo, but i'm reading the data in as an object using readObject(), if it wasn't specifically stated in the assignment sheet to use it, i would of read/write by text... the first person who answers gets a mintie :)
its ok :) another stupid error!
for those interested
public static void getCars(File data, LinkedList carList)
instead of just List car!
but thanks everyone for their help
Yes, I see that, but you can adapt what I did to read Cars instead of Strings.
I thought your problem was that the List wasn't updating, and I demonstrated that it works.
I'm not going to write your program for you. But I figured you could look at something that WAS working and figure out why yours wasn't. - MOD
"assignment sheet" - ah, now I get it. I'm doing your homework for you. - MOD
i wish that was all this assignment was :) its pretty big, i'm at about 900 lines of java :( so far! can't wait till i finish it! thanks for your help as well :)
I'd suggest a signature of
public static void getCars( File data, List carList )
Notice I used List instead of LinkedList. You should always declare/code to the interface (i.e. List) not the implementation (i.e. LinkedList). It makes maintenance much easier. I know you probably aren't concerned with it now, but it's a very good habit to get into.
Jamie
but it didn't work with List... only LinkList... why's that?
The example code I posted works with List (see method signature). What are YOU doing wrong? - MOD
In about post 5 I made this point about changing the signature and it would have all worked fine...but obviously guy has no clue or inclination to learn as it's only homework and duffymo can be duped into doing it so why bother learning! Will still get a job if he gets 2.1!
> In about post 5 I made this point about changing the
> signature and it would have all worked fine...but
> obviously guy has no clue or inclination to learn as
> it's only homework and duffymo can be duped into doing
> it so why bother learning! Will still get a job if he
> gets 2.1!
if i didn't want to LEARN i wouldn't be enrolled at uni... if i don't understand whats happening then excuse me.. i'm new at this.. i would of loved to be inborn with this knowledge but unfortunatly this is all new to me.. i'm here to ask for help... i changed the sig and it didn't work! it won't allow List but will with LinkedList, i don't know why.. but i'd prefer you didn't jump on me!
signed!
mark
If you declare it like this:List carList = new LinkedList(); and you use only methods defined by the List interface, it should work just fine. What do you mean when you say "it didn't work with List"?
> if i didn't want to LEARN i wouldn't be enrolled at> uni... Thats funny! Most people go to universities to make more money, not to learn.
> If you declare it like this:> List carList = new LinkedList();
and you use
> only methods defined by the List interface, it should
> work just fine. What do you mean when you say "it
> didn't work with List"?
i hadn't declared the list like that, all i did was
LinkedList car = new LinkedList()
so when i tried to pass it to a method it was having a fit..
i didn't even know i could use List instead of LinkedList (not in my lecture notes) what are the advantages of using List instead of LinkedList?
and i'm at uni to make money as well :) but i wouldn't be doing this course if i didn't want to do it.. i would of been pov and done a buisness managment or ARTS degree...
Perhaps your List was being confused with java.awt.List?
You want to use the interface whenever possible.
That allows you to change the implementation easily in one location
instead of everywhere.
import java.util.List;
...
List listCars = new LinkedList();
...
public void doSomething(List listObjects)
Then when you find that incredible new List implementation,
you simply change one spot:
List listCars = new SuperIncredibleTripleRedundantTreeList();