954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Custom object to store various types

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 :)

TahoeSands
Light Poster
34 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

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...
}
coil
Posting Whiz in Training
273 posts since Aug 2010
Reputation Points: 27
Solved Threads: 56
 

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;
     }
 }
tong1
Posting Whiz
358 posts since Jul 2010
Reputation Points: 34
Solved Threads: 72
 

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.

TahoeSands
Light Poster
34 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

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.

TahoeSands
Light Poster
34 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

Very simple and very elegant example.

bunifrog
Newbie Poster
12 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: