• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

array with elements of different types?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Vince Aggrippino
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ....

 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
 
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic