aspose file tools*
The moose likes Java in General and the fly likes How can I manually add values to ArrayList[] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How can I manually add values to ArrayList[]" Watch "How can I manually add values to ArrayList[]" New topic
Author

How can I manually add values to ArrayList[]

Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

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


True wisdom is in knowing you know nothing - Socrates
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

Hmm

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 well...lol, 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();
projectDevices.add(project.getDeviceId));
devices.add(projectDevices);

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?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3971
    
  17

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.


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I manually add values to ArrayList[]
 
Similar Threads
need suggestions on how to deal with data iteration problems
Help extracting text out of a response
really need some help here concerning treeNode data types and the like. (data type issues really)
Trying to add Object values to a List, but the last value keeps getting added for all the values
Trying to bind inputText fields from an a4j:repeat tag to java code