Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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 ...
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

How can I manually add values to ArrayList[]

Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this in a class:

private ArrayList[] devices;

I need to know how to add values to this devices ArrayList.

when I type devices. nothing pops up after the period (I expected a .add or something like that)

This is only one small step to making this do what i actually need it to. I have a jsf page where a user enters something into a text field (a String). Then I have some xml, one of the lines being this:

<devices class="java.util.ArrayList" />

under that tag will be <device>somevalue</device>
So that is why I have declared ArrayList[] devices. The ultimate goal is to take whatever the user enters in the text field, assign that to SrrayList[] devices, which will then post to the web service under the <devices class="java.util.ArrayList" /> tag.

But for now, I just want to manually assign some text value to ArrayList[] devices. Can soeone tell me how to do so please?

edit - the probelm is I guess, I have a setDevices method. But the only thin I can pass to this method is an ArrayList[]. The problem with that is that the user is inputting a String, not an ArrayList[]. So it seems simple, but all I need to happen is to take the String the user inputs and add it to the ArrayList[] devices variable
Posts: 67650
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code:does not declare an ArrayList. It declares an uninitialized array of ArrayLists. Probably not what you wanted.

To create a new ArrayList:You will then be able to add elements to this list using its .add() method.
Matt Kohanek
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am pretty sure that ArrayList[] devices is what I need, although I see now that that would be an arrylist of arraylists.

The reason it has to be this way though, is because here is the xml this variable relates to:

So you can see there that the device variable is referring to the devices node there in my xml, and that devices node holds two other nodes (projectDevice and deviceId).

Let me see if I can put this more clearly.

I have a jsf page where a user can enter in a project name and a deviceId. The project name is then posted to the <name> node in the above xml. But the deviceId is a different story. This deviceId is posted to the following xml:

Now I dont have trouble with this. But this device needs to know it belongs to a certain project, and that is where this part of the projects xml comes in:

So some how, I need the deviceId to be added to the deviceCollection (which it does), but then also be added to the devices there in the project xml. I wish I could articulate this more clearly lol!

It is simple to bind the device and project text fields to the correct bean, and then have the submit button call the method to add these strings to the web service. But I now need to add the deviceId to that section of the projectCollection as, I know it is hard to understand what i am trying to say here

edit - ok, so let me think about what this means.

I add the deviceId as a String to the deviceCollection. So far so good. Now I need to add this to an aray as well, and then I can add that array to the projectCollection.

so it would have to be something like:

ArrayList[] devices;
List projectDevices = new ArrayList();

Hmm, I know that has to be soemwhat close. But how will it know to add this to devices in the correct manner? Man this nesting stuff, why cant I have started with something simpler

nope this is still giving me a mismatch. How do I add an arrayList to an ArrayList?
Posts: 4179
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Henry said:

This is not an ArrayList, nor is it an ArrayList of ArrayLists. It is an Array of ArrayLists, and it is uninitialized.

1) An Array is not the same as an ArrayList
2) Arrays have a fixed size. You have to initialize them to a specific size. Once you have initialized them you can then fill the Array (in this case with ArrayLists).
3) In order to reference one particular ArrayList inside the Array, you need to access it via an index.

For example, if I wanted to make an Array of ArrayLists, then I would have to know the count of ArrayLists I wanted. So maybe I wanted 3:

Now the devices Array is initialized, but it is still empty. I have to fill the Array:

That code inside the loop uses an index (i) inside devices (devices[i]), and makes a new ArrayList.

Then I can add something to the List as some index:

And just to be clear:
Each of the devices indexes is an ArrayList and would represent one of the devices XML elements as defined like:

All that being said, in order to use an Array you need to know how many <devices ...> elements you are going to get. If you don't then you can use a real ArrayList of ArrayLists. This would look something like:

There are better ways to handle this, probably. Something more Object Oriented but I don't really understand the structure so I will avoid guessing.
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic