How do I copy certain File elements in an array to a new File array?

Go To StackoverFlow.com

0

This is it possible to implement this? I have File foo[] that has a list of 5 files. But I only want to copy foo[0] and foo[3] into File bar[] so that bar[] will only have 2 elements.

My code gets the length of foo[], then if the index of the selected file equals the index for i, add foo[i] to bar[i]. This is the possible code I've constructed:

for(int i = 0; i < foo.length; i++){

    if(list_fileListing.getSelectedIndex() == i){
        bar[i] = foo[i];
    }
}

*list_fileListing.getSelectedIndex() holds the list of selected files from the JList.* The reason being is that I have a list of files that are selectable in a JList. And from that list, I want the user to be able to select which files to attach to an email.

2012-04-05 03:11
by SpicyWeenie
to be clear: you have a JList where the user selects some files, and you want a second array which contains only those files - Dan O 2012-04-05 03:20
@orzechowskid Yes, because I only want the arrays that are selected to be attachable to emails. To be more clear about my reasons, look at this link where Rodricks george posted how to send attachments to emails: http://www.coderanch.com/t/273794/java/java/send-email-multiple-attachments I've tested it, but it sends every file in a directory - SpicyWeenie 2012-04-05 03:23


2

According to your comments, your JList contains String instances and not File instances. So you could do something like

List<String> selectedFilesAsStrings = list_fileListing.getSelectedValuesAsList();
//selectedFilesAsStrings will never be null, but can be empty
List<File> selectedFiles = new ArrayList<File>( selectedFilesAsStrings.size() );
for( String fileName : selectedFilesAsStrings ){
  selectedFiles.add( new File( fileName ) );
}
File[] bar = selectedFiles.toArray( new File[ selectedFiles.size() ] );

which will set the bar array pointing to an array containing the selected File instances

2012-04-05 06:01
by Robin
Thats the form I wanted. I just now have to rework my other errors got. Nevertheless, this helped me a lot. Thanks! : - SpicyWeenie 2012-04-05 08:14


2

if you call getSelectedValues() on your JList, you will get an array containing all items currently selected:

Object[] selectedObjects = list_fileListing.getSelectedValues();

for (int i = 0; i < selectedObjects.length; i++)
{
    File aFile = (File)selectedObjects[i];

    // attach this file
}

is this sufficient?

2012-04-05 03:34
by Dan O
Didn't work... I got this error Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException where the for-loop is, plus "getSelectedValues() is deprecated - SpicyWeenie 2012-04-05 04:10


2

It looks like you're using Java7 since you have JList.getSelectedValues() deprecated. Try using getSelectedValuesList() method instead. If you need an array you can use list.getSelectedValuesList().toArray()

2012-04-05 04:50
by tenorsax
yeah, I have Java7. that didn't work either. I get: Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.io.File - SpicyWeenie 2012-04-05 05:48
@チョコレート人, that is probably because your list contains String objects and you're casting it to File. Can you show the code that initializes the list - tenorsax 2012-04-05 05:56
Ads