简体   繁体   中英

Dynamic arrays in Java

I am creating a directory traversal application for sorting emails. It is a very simple app for my specific needs.

I would like to implement this by first traversing all the email files (there are 1000's of them in a few directories) and make new directories based on the sent/received information inside the eml files (The sent or received name used will be decided based on a constant).

I was thinking something like this

public FileVisitResult visitFile(Path file,BasicFileAttributes attrs) {
    email = getEmail(file);
    emailSentRcievedName = sentRecievedName(email);
    dirArray[currentDir + "\" + emailSentRecievedName] = file.get();
    return CONTINUE;
}

The dirArray key:value will be used for the file moving implementation.

I know this array structure would work in Javascript but how would I implement this in Java?

Cheers.

Try using a hashmap. It gives you the ability to store pairs of data

Map<String,File> map = new HashMap<String, File>();
map.put(currentdir +"/"+ emailSentReceivedName, file);

File file = map.get(path); //will give you the file

Also take a look at this: SortedMap , maybe it helps you http://docs.oracle.com/javase/1.4.2/docs/api/java/util/SortedMap.html

Javadoc api here: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/HashMap.html

请改用java.util.HashMap()!

Just use a HashMap :

Map<String, File> map = new HashMap<String, File>();
map.put(currentdir + "\" + emailSentReceivedName, file.get());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM