Re: Math.random()
maya wrote:
Jeff Higgins wrote:
Daniel Pitts wrote:
maya wrote:
(I want to have option of displaying photos in a random order
instead of in order of img-names (1.jpg, 2.jpg, 3.jpg, etc..))
Try putting the photos into a List object, and then calling
Collections.shuffle() with that list object as the parameter.
A clear winner. :-))
ok, am trying to do this now.. acc to docs it's like a vector except
"synchronized" (not sure what this means..) only thing in Vector you add
elements thus:
vPhotos.addElement(imgsList[i]);
but am having problems here:
ArrayList photos = new ArrayList();
File directory = new File(System.getProperty("user.dir"));
String[] textFileNames = directory.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".jpg") || name.endsWith( ".JPG");
}
});
for (int i=0; i < textFileNames.length+1; i++) {
photos.add(i); // error here...
}
how do I convert this "i" to an object (since this method takes only obj
as argument..)
and then how do I apply Collections.shuffle() to it??
photos.Collections.shuffle() ?? I dont think so....;)
I really need this today, for some photos I'm putting together for a
friend overseas, sending with a friend who travels today....
thank you very much..
ok, I think got Collections.shuffle() part..
Collections.shuffle(photos, new Random());
only problem now is how to add all nos. to ArrayList (this has to be 1.4
construction, not 1.5..)
and then how to go from Collections.shuffle to this:
for (int i = 0; i < textFileNames.length; ++i) {
File oldFile = new File(textFileNames[i]);
posDot = textFileNames[i].indexOf(".");
_temp = textFileNames[i].substring(0,posDot);
File newFile = new File(i+1 + ".jpg");
oldFile.renameTo(newFile);
}
so imgs are named with nos. at random (i.e., so imgs, after renamed, are
in diff order from orig ones..)
thank you....