• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arraylist object naming  RSS feed

 
Ivan Marik
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys! This is my first post I'm fairly new to programming. I've done a little bit of c#, VB and Java. I'm having an issue in figuring out how to name an array list that I am creating inside a method using a string parameter that will be set to some user input. I've researched a little bit and I'm having a hard time finding what I'm looking for. It seems to me that there would be a method for it in the ArrayList() but I can't find one on the API site. Thanks for any help or advice maybe someone with better researching skills could point me in the right direction. Thanks again!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Marik wrote:Hey guys! This is my first post I'm fairly new to programming. I've done a little bit of c#, VB and Java. I'm having an issue in figuring out how to name an array list that I am creating inside a method using a string parameter that will be set to some user input. I've researched a little bit and I'm having a hard time finding what I'm looking for. It seems to me that there would be a method for it in the ArrayList() but I can't find one on the API site. Thanks for any help or advice maybe someone with better researching skills could point me in the right direction. Thanks again!


Elements of an ArrayList are organized by indexes. It is not possible to "name" an element. Perhaps you are looking for some sort of Map -- that saves values in key/value pairs? With this, you can use a string name as the key.

Henry
 
Ivan Marik
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not trying to name the elements. I'm trying to name the playlist which is an ArrayList that is basically assigned a string name when created so It seems like I should be able to take in a string parameter and assign it to the list I create in my method. Ill look into the map class and see if it will help. Thanks for the quick response!
 
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
Ivan Marik wrote:I'm not trying to name the elements. I'm trying to name the playlist which is an ArrayList that is basically assigned a string name when created so It seems like I should be able to take in a string parameter and assign it to the list I create in my method. Ill look into the map class and see if it will help. Thanks for the quick response!


You need to elaborate some more -- not sure what you are talking about.

Henry
 
Ivan Marik
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to create a method that will take in a string variable as a parameter and will assign the string variable to the object reference for a new arraylist that will be my playlist of song objects. The object reference is essentially just a string that points to an object so that's why I'm having a hard time understanding why this is so difficult but it seems to be.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Marik wrote:I want to create a method that will take in a string variable as a parameter and will assign the string variable to the object reference for a new arraylist that will be my playlist of song objects. The object reference is essentially just a string that points to an object so that's why I'm having a hard time understanding why this is so difficult but it seems to be.



How about creating a new class? A playlist class that has a string for the name, and an arraylist for the songs in the playlist? And the methods to manipulate the playlist?

Henry
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like you want to create a variable with a name that is passed in as a string. Something like this:

Does that look like what you want to ask? (You'd not be the first person asking a similar question).

If that's it, then the answer is: it doesn't work that way. (That explains why it seems to be so difficult...).

Variables have names, but those names are only used by the programmer and the compiler. When the program runs, these names don't even exist anymore. Instead, you can use a Map to store things by name. For example:

 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!