Solved

Arraylist into  public void add(String fileName) {

Posted on 2004-03-31
6
376 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

910 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

19 Experts available now in Live!

Get 1:1 Help Now