This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how do I make an array like this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how do I make an array like this?" Watch "how do I make an array like this?" New topic

how do I make an array like this?

colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

How could put all of the gif images in a directory into an array of ImageIcons? Would it be something like:

ImageIcon[] imageArray = ImageIcon["C:\pixs\*.gif"];

Everything I try comes up with a imcompatible type error.

Any help would be appreciated.
Robert Hill
Ranch Hand

Joined: Feb 24, 2006
Posts: 94
ImageIcon[] imageArray = ImageIcon["C:\pixs\*.gif"];

The array declaration is looking for an integer to use for its size, not a String.

Secondly, passing a String into an array element won't work.A String is definitely NOT an ImageIcon. So this will not work either

ImageIcon[] imageArray = ImageIcon[100];


imageArray[0] = "C:\pixs\*.gif"

Hint: Look up ImageIcon in the API docs.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14950

One more thing: In a Java string literal, the backslash is used as an escape character. To include a literal backslash in a string, you need to use \\, so it should be: "C:\\pixs\\*.gif"

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor

Joined: Oct 27, 2005
Posts: 20077

[ November 20, 2007: Message edited by: Rob Prime ]

How To Ask Questions How To Answer Questions
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

Thanks. One more thing, when he said:

icons[i] = new ImageIcon(files.getAbsolutePath());

he didn't specify what element in the array he was using. Can you do that? Wouldn't it be more like:

icons[i] = new ImageIcon(files[i].getAbsolutePath());
[ November 20, 2007: Message edited by: colton peterson ]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

Yes -- that was a typo.

[Jess in Action][AskingGoodQuestions]
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: how do I make an array like this?
It's not a secret anymore!