I am working on an application in Java and for part of my plan I would like to have an object (eventually an array of these objects) where I could store two or three (I am not sure yet) varied types of data, for instance...

A BufferedImage and a string that is the "name" of that image.

I have not created any objects of my own design yet, so my experience level is pretty low.

Thanks,
TahoeSands

PS I do real well with simple examples :)

Recommended Answers

All 6 Replies

Member Avatar for coil

If you're wondering how to do it, you need rudimentary knowledge of classes, which is a pretty big subject. It'll look something like this though:

class CustomImage {
private BufferedImage image; // The image
private String name; // Stores the name

public CustomImage(BufferedImage b, String s) { // Constructor
image=b;
name=s;
}

// Methods...
}

An array is a container object that holds a fixed number of values of a single type.” (true for some cases only)
http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
In broad sense, I would say:
“An array is a container object that holds a fixed number of values of either a single type or different types/multi-types.” In other words, in the Java programming language arrays are objects, are dynamically created, and may be assigned to variables of different types .
http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
For example, the following code shows that the Object array object contains values of 4 different types: ImageIcon, String, BufferedImage and int.

import javax.swing.*;
import java.awt.image.*;
class Array{  	
  	public static void main(String []args){
    Object object[]=new Object[4];
    object[0]=new ImageIcon("ABC.png");
    object[1]="Image Icon ABC";
    object[2]=new BufferedImage(400,400,BufferedImage.TYPE_3BYTE_BGR);
    object[3]=34;
     }
 }

If you're wondering how to do it, you need rudimentary knowledge of classes, which is a pretty big subject. It'll look something like this though:

class CustomImage {
private BufferedImage image; // The image
private String name; // Stores the name

public CustomImage(BufferedImage b, String s) { // Constructor
image=b;
name=s;
}

// Methods...
}

I suppose I had this knowledge in my head, but I am new enough to not be able to pull it together without an example or a hint at this point. You're example was perfect, just enough of a boost to get over the wall. The knowledge I now have is this... A custom object and a class are exactly the same thing.

An array is a container object that holds a fixed number of values of a single type.” (true for some cases only)
http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
In broad sense, I would say:
“An array is a container object that holds a fixed number of values of either a single type or different types/multi-types.” In other words, in the Java programming language arrays are objects, are dynamically created, and may be assigned to variables of different types .
http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
For example, the following code shows that the Object array object contains values of 4 different types: ImageIcon, String, BufferedImage and int.

import javax.swing.*;
import java.awt.image.*;
class Array{  	
  	public static void main(String []args){
    Object object[]=new Object[4];
    object[0]=new ImageIcon("ABC.png");
    object[1]="Image Icon ABC";
    object[2]=new BufferedImage(400,400,BufferedImage.TYPE_3BYTE_BGR);
    object[3]=34;
     }
 }

Great... Excellent example. I have lots of ways to go now. I just need to decide which fits my application the best.

Very simple and very elegant example.

Beware the array "solution". It can be made to work, but its as un-Java as you can imagine. Youcan stuff anything into an array of Objects, but then you have to do unchecked casts to get them back and use them. There's no opportunity to hide the data behind a set of accessors/mutators, so it fundamentally violates the OO principle of hiding the data/implementation. It's horrible.
Creating a class is the standard way to go.
Your example (Image and name) suggests that maybe you will want to access the image via its name? In which case, have a look at the HashMap class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.