Solved

Arraylist into  public void add(String fileName) {

Posted on 2004-03-31
6
375 Views
Last Modified: 2010-03-31
I have the files in an arraylist. How would I get them from the arraylist into the code below, this first section, is the bottom section of the code following it.



 public void add(String fileName) {
        listOfFiles.add(fileName);
    }


public class ListOfFiles implements Enumeration {

    private List listOfFiles = new ArrayList(); // Start with an empty list by default
    private int current = 0;

    public ListOfFiles() {
    }
    public ListOfFiles(List listOfFiles) {
        this.listOfFiles = listOfFiles;
    }

    public boolean hasMoreElements() {
        if (current < listOfFiles.size())
            return true;
        else
            return false;
    }

    public Object nextElement() {
        InputStream in = null;
        if (!hasMoreElements())
            throw new NoSuchElementException("No more files.");
        else {
            String nextElement = (String)listOfFiles.get(current);
            current++;
            try {
                in = new FileInputStream(nextElement);
            } catch (FileNotFoundException e) {
                System.err.println("ListOfFiles: Can't open " + nextElement);
            }
        }
        return in;
    }

    public void add(String fileName) {
        listOfFiles.add(fileName);
    }
}
0
Comment
Question by:Drop_of_Rain
  • 2
  • 2
  • 2
6 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 10726960
public void add(ArrayList fileNames) {
        for (int i=0;i<filesNames.size();i++) {
            lstOfFiles.add((String) fileNames.elementAt(i));
        }
    }
0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 10727052
listOfFiles.addAll(otherlist);
0
 

Author Comment

by:Drop_of_Rain
ID: 10727217
That is a big difference of simplicity. They both work so how do I handle this question reqarding the points. I prefer objects answer though.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 18

Expert Comment

by:bobbit31
ID: 10727236
give points to objects, his answer is clearly better
0
 

Author Comment

by:Drop_of_Rain
ID: 10727358
objects I really like the way you approach your code writing, direct but simple. I hope I learn from your example.

Rgds,
Christopher
0
 
LVL 92

Expert Comment

by:objects
ID: 10727412
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now