• Post Reply Bookmark Topic Watch Topic
  • New Topic

storing integers and string together in an arraylist  RSS feed

 
Michael Ayers
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an assignment where I have to describe a realistic scenario for using either an array or arraylist. I was going to use a program for storing data about a firearms collection using arraylist because its sizes dynamically but I need to store strings as well as integres. is this possible and if it is, how? Thank you for your help.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Ayers wrote:I have an assignment where I have to describe a realistic scenario for using either an array or arraylist. I was going to use a program for storing data about a firearms collection using arraylist because its sizes dynamically but I need to store strings as well as integres. is this possible and if it is, how? Thank you for your help.


First, I am not a fan of storing different types in a collection, but ... if you have to do it ... Integers and String classes have a common ancestor, so technically, you can store them in an ArrayList<Object> collection.

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WHY do you have to store both in the same arraylist? I would guess that your reasoning for this is wrong. If you explain what you really want to do, I'm sure we can come up with a better solution.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
First, I am not a fan of storing different types in a collection, but ... if you have to do it ... Integers and String classes have a common ancestor, so technically, you can store them in an ArrayList<Object> collection.

Agree on not storing different types.
Considering the OPs requirement instead of List<Object> maybe it would make more sense to have List<Firearm>
 
Michael Ayers
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys. I want to store data items like manufacturer, serial number, caliber etc., so need strings and integers. I know that arraylist stores objects so if I did make an object ArrayList<firearms> I could store all of the data together. I wanted to use ArrayList because I don't know the size of the list, it would grow and shrink as objects were bought or sold. Thank you, I believe I will be using this site quite a lot.
Mike
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Ayers wrote:Thank you guys. I want to store data items like manufacturer, serial number, caliber etc., so need strings and integers. I know that arraylist stores objects so if I did make an object ArrayList<firearms> I could store all of the data together.

Note:
1. It would be ArrayList<Firearm>, not ArrayList<firearms>, because
(a) Each object is a single firearm; it's the list that can hold lots of them.
(b) Types (ie, classes) in Java always begin with a Capital Letter - it's only a convention, but it's almost universally adopted; so start using it NOW.

2. Whenever you have a bunch of stuff that is going to be used together, put them in a class (in your case 'Firearm'). You''l find that you can rarely have too many classes, but you can certainly have too few; so don't be afraid to bash out new ones.

HIH

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Ayers wrote:Thank you guys. I want to store data items like manufacturer, serial number, caliber etc., so need strings and integers.

I still don't understand. What good does it do to have an array list with this in it:

Smith and Wesson
Glock
Kalishnikov
22
123abidcm32789
44
Colt
17

You have no way to know what those things MEAN if you just pull them out. If you are planning on always inserting three (or whatever) values at a time (manufacturer, serial number, and caliber), and then always reading them out three at a time...that is a BAD design. You will inevitably get to a point where you only insert 2, or one element gets deleted, and then your arraylist is garbage.

Data that belongs together should be stored together. Create a Firearm class that has member variables to hold the related values, and then store THOSE in your arraylist. That way, you pull out ONE thing, and have all the data you need for it.

 
Michael Ayers
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you again. I hadn't thought of that. This post is for a class discussion so I chose to use ArrayList over a multi-dimensional array because of the specified size of an array vs ArrayList dynamic sizing capabilities. But you have given me more to think about if I have to write a program like this.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nobody is arguing the use of an arrayList vs. an array. I'm pretty sure everyone would agree with that decision.

Something else to be aware of...Java does not have multi-dimensional array, only single-dimensional arrays. However, an array can hold anything, including arrays. So what you think of as a 2-d array is really an array of arrays. What you think of as a 3d array is really an array that holds arrays....of arrays.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

If that was a discussion in class, I am surprised nobody suggested a Gun class or similar, as Fred suggested earlier.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!