Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > return a Collec...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 11 Topic 16078 of 16477
Post > Topic >>

return a Collection (Set or List)

by thufir <hawat.thufir@[EMAIL PROTECTED] > May 13, 2008 at 11:29 AM

The scanFile method returns a List.  Sometimes, however, I want it to 
return a Set.  When I change the return type to Collection I get an error:

a00720398/data/BedAndBreakfast.java:33: incompatible types
found   : java.util.Collection<a00720398.data.Guest>
required: java.util.List<a00720398.data.Guest>
        guests = console.scanFile(new File("Guests.txt"), new GuestFactory
());
                                 ^
1 error


I've seen Collection used as a parameter in a signature, can it also be 
used as a return type?  A quick Google seems to show "yes".  Is there a 
better way of getting the most out of this method?



thufir@[EMAIL PROTECTED]
 
thufir@[EMAIL PROTECTED]
 cat src/a00720398/ui/Console.java
/**
 * Console.java
 */

package a00720398.ui;

im****t java.util.*;
im****t java.io.*;
im****t a00720398.data.*;
im****t a00720398.interfaces.*;
im****t a00720398.util.*;


public class Console {

        public Console(){}

        public <T> List<T> scanFile(File file, Factory<T> factory) {
                List<String> lines = new ArrayList<String>();
                List<T> list = new ArrayList<T>();
                try {
                        Scanner scanner = new Scanner(file);
                        while (scanner.hasNextLine()) {
                                String line = scanner.nextLine();
                                        try {
                                                T object = factory.make
(line);
                                                list.add(object);
                                        } catch (DataException e) {
                                                e.printStackTrace();
                                        }
                        }
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                }
                return list;
        }

        public void output(List<?> objects, File f){
                try {
                        RandomAccessFile file = new RandomAccessFile(f, 
"rw");
                        try {
                                file.seek(file.length
());                               //go to end of file
                                file.writeUTF(objects.toString());      //
append the collection
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                }
        }

}
thufir@[EMAIL PROTECTED]
 
thufir@[EMAIL PROTECTED]
 




thanks,

Thufir
 




 11 Posts in Topic:
return a Collection (Set or List)
thufir <hawat.thufir@[  2008-05-13 11:29:57 
Re: return a Collection (Set or List)
Lew <lew@[EMAIL PROTEC  2008-05-13 08:18:51 
Re: return a Collection (Set or List)
Roedy Green <see_websi  2008-05-13 12:40:58 
Re: return a Collection (Set or List)
Mark Space <markspace@  2008-05-13 11:37:53 
Re: return a Collection (Set or List)
Roedy Green <see_websi  2008-05-13 19:39:07 
Re: return a Collection (Set or List)
Eric Sosman <Eric.Sosm  2008-05-13 16:13:07 
Re: return a Collection (Set or List)
Lew <lew@[EMAIL PROTEC  2008-05-13 19:41:37 
Re: return a Collection (Set or List)
Roedy Green <see_websi  2008-05-14 08:22:59 
Re: return a Collection (Set or List)
thufir <hawat.thufir@[  2008-05-13 22:38:12 
Re: return a Collection (Set or List)
RedGrittyBrick <RedGri  2008-05-14 10:27:23 
Re: return a Collection (Set or List)
Lew <lew@[EMAIL PROTEC  2008-05-14 08:07:30 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Oct 11 12:16:00 CDT 2008.