Solved

Arraylist into  public void add(String fileName) {

Posted on 2004-03-31
6
389 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

707 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