• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to create an array with multiple data types ?  RSS feed

 
mitchell cooper
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please oh please can i have some help, i am desperately trying to figure out how you wold go about making an array list with multiple data types. i want to create a table that would look like this...

color quantity price
pencil red 6 100
pen blue 11 250
rubber orange 28 380

i may of asked a question that could have a wide range of answers but its a shot in the dark, i guess im asking what would i use arrays, arraylist, hashmaps or what, where do i start ?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
None of the above. I'd create an object called Item (or something similar) that had three instance variables. Then I'd have an ArrayList<Item>.

 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be specific in your questions. Your title asks about arrays but in the text you mention array lists. The two are different. But Jeanne's answer will apply to both of them.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in Java, arrays can hold one kind of thing - and only one kind. Now, that thing may itself hold multiple things, as Jeanne suggests (and she is very smart - you should pay attention to anything she tells you).
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your different types need to all extend the same class, then you can use generics to declare the type of Array, ArrayList, or other containers you want to use for your implementation. I have a personal preference for ArrayLists when tracking collections of objects.
 
mitchell cooper
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou for your replies, i know my questions may be stupid or not to the point but it seems learning programming you need to ask the right questions but starting out its hard to find the right questions lol. The goal i am aiming for is to have a list of items and certain statistics for each it for example... Item Wood, Quantity 20, Strength 400. But later the values can be changed at a later date by user input. I am not looking to code the user input yet just want to know how i would set the list up ?
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mitchell cooper wrote:The goal i am aiming for is to have a list of items and certain statistics for each it for example... Item Wood, Quantity 20, Strength 400. But later the values can be changed at a later date by user input. I am not looking to code the user input yet just want to know how i would set the list up ?


Exactly as Jeanne said: write a class called Item that has as its fields a name, colour, quantity, and price (or strength as you mentioned above? Whatever attributes you want an item to have). When you read the items in (from a file, user input, whatever), you would extract all those attributes, build a new Item with those attributes, and add that Item to your list:

 
mitchell cooper
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i have written i out now i am stuck on constructor how would i go about this sorry for wasting your guys time i just need this to click so i can look through it and play with it, everyone has to learn some way lol her is what i have written.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all some comments on your code:
The Java naming convention is to start variables with lower case letters.
Are you sure you want to use Double (or double) to hold a price. Please google for something like "why not use floating point numbers for currency" to find out why this is a really bad idea.

As to your question - the same way you write any constructor for any class? The constructor must have the same name as the classname, must not have a return type and must have the correct number and type of parameters in the correct order. That is it must be called Item and have 3 parameters which will be a String, an int and a Double (assuming you don't take my advice on not using Doubles) in that order.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another comment...and this is something I know I certainly had a hard time getting my head around...

Your Item class doesn't need (and probably shouldn't have) a main method. you should write some other class that is the real driver of your program, and it will create Items...I know we're throwing a lot at you, but something else to consider reading is our Main is a Pain FAQ.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!