aspose file tools*
The moose likes Beginning Java and the fly likes array with elements of different types? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array with elements of different types?" Watch "array with elements of different types?" New topic
Author

array with elements of different types?

Vince Aggrippino
Greenhorn

Joined: May 19, 2006
Posts: 14
I'm working on my first Java program, for practice. It's a text editor and I want to be able to have a configuration file to store settings like font, color, window size, etc... I'm not having any problem with opening/reading the file or creating the interface for changing the settings. However, I'd like to store all of these settings in one variable.

Array probably isn't the right thing. Is this a Collection? I have lots of reading materials, but no experience.

For example, my settings include Objects for a Font (just one, I'm using JTextArea for now), several Colors (foreground, background, etc.), several integers (window width/height), and several boolean flags (remember size/location of window).

Thank you,
Vince


-- Vince
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
Create a Class called Config and store your values as key/value pairs on a HashMap within your config class and provide accessor methods read relevant data.


java j2ee job interview questions with answers | Learn the core concepts and the key areas
Vince Aggrippino
Greenhorn

Joined: May 19, 2006
Posts: 14
Thank you. I've read through the API for HashMap and that is the best solution in this case.

My original question wasn't well thought-out. I wanted to store objects of different types in an array, but I'm reading them from a text file, so they have to start out as Strings anyway.

I'm still curious, though... Is there a type of array that can contain elements that aren't all the same type? I mean, with HashMap, it looks like I can have an associative array with keys of one type and values of another type, but those values would all have to be of the same type. If it was , all of the values have to be Integers, don't they? I couldn't , could I?

Thank you,
Vince
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by Vincente Aggrippino:
Thank you. I've read through the API for HashMap and that is the best solution in this case.

My original question wasn't well thought-out. I wanted to store objects of different types in an array, but I'm reading them from a text file, so they have to start out as Strings anyway.

I'm still curious, though... Is there a type of array that can contain elements that aren't all the same type? I mean, with HashMap, it looks like I can have an associative array with keys of one type and values of another type, but those values would all have to be of the same type. If it was , all of the values have to be Integers, don't they? I couldn't , could I?

Thank you,
Vince



Actually what i understand about your question is, you want to store any type of information in HashMap.


, all of the values have to be Integers, don't they? I couldn't

there is no constructer in HashMap through which you can decide what the element types in HashMap should be..

Actually if you have gone through HashMap API's then you must know that HashMap stores data in format.



So if you want to store any primitive data and you are using Jdk1.4 then you have to typecast it in it's Wrapper Class object(In jdk1.5 it's done automatically by autoboxing).. else otherwise everything in JAVA is in form of Object..

That's solve your problem..

Hope it helps you.

if still any concern then revert back ....



The Best way to predict your future is to create it - Every great individual common man
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296

there is no constructer in HashMap through which you can decide what the element types in HashMap should be..
Actually in Java 1.5 it is possible to specify what types are stored in a HashMap (or most any other Collection) using generics. This is generally regarded as a *good thing* as it offers compile time type checking which guarantees type safety. The class source is declared as


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
i think, a Doubly linked list(collection) would be better, thats just me,

because each node can have several values that you can store in each node..

and the next and prev pointers, would help you access these nodes easily..

throw in a findinList() method, and you're done..

you can findinList(Color a);

findinList (int width);

anything...

and the cool thing about linked lists (doubly) is that you can, in a way,

just link the values together, for instance..



then you can add your addNode() method, and you can also overRide the method, so you can addNode(int), addNode(int, color), addNode(color);

Justin;


You down with OOP? Yeah you know me!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array with elements of different types?