tags:

views:

170

answers:

7

here i am trying to add elements to the array. the elements i am trying to add are text fields, so im basically trying to store persons contact details within the array list?

any help would be greatful

public void addContact()
{
    ArrayList<String> details = new ArrayList<String>();
    {
        details.get(txtname(0));
        details.get(txtnum(1));
        details.get(txtmob(2));
        details.get(txtadd1(3));
    }        
}
+6  A: 

It sounds like you haven't thought out the entire problem yet.

Adding elements to an ArrayList in Java is done like this:

public void addContact(){
    ArrayList<String> foo = new ArrayList<String>();
    foo.add("HELLO");
    foo.add("WORLD");
}
yankee2905
A: 

You need set or add, not get. See the docs here.

And to get the text from the textfields, use getText.

So you'd have something like:

myArrayList.add(myTextField.getText());
JRL
A: 

You are trying to use inbuilt array initializer syntax. That does not work on container classes (unless its some new fangled way in c#) you need to use details.add() (or the appropriate member function).

The syntax you are trying to use is for the language supported hardwired array types. In C++ this would look like char x[6] = {'h','e','l','l','o'};. However a container is not an array its a container object. Container objects often mimic arrays by overloading operator[] however they use different data structures behind the scenes -- i.e., they do not use contiguous regions of memory.

p.s., If this was c#.NET -- which I initially assumed -- there is a new mechanism to map array initialization to container object creation. I'll leave it down there for anyone that is interested.

in C# 3.5 using array initializer syntax you can do the following :

public void addContact()
{            
    ArrayList<String> details = new ArrayList<String>()
    {
        details.get(txtname(0)),
        details.get(txtnum(1)),
        details.get(txtmob(2)),
        details.get(txtadd1(3))
    }        
}

Gotta love Microsoft and C# :P

Hassan Syed
This does not make any sense in Java - even after your edits. The OP is not using anything remotely like Java's array initializer syntax.
Stephen C
+2  A: 

yankee2905 explains it very well; that's what you need to get your code to work with an ArrayList.

As a side note, you're not dealing with an array, you're dealing with an ArrayList. For an array, you might have something like this:

String[] details = new String[4];
details[0] = "First";
details[1] = "Second";
details[2] = "Third";
details[3] = "Last";
Dean J
A: 
public void addContact()
        {
          ArrayList<String> details = new ArrayList<String>();
          {
          details.add(//Insert value from post here);
          details.add(//Insert value from post here);
          details.add(//Insert value from post here);
          details.add(//Insert value from post here);
          }        
        }

I've not used java in a while maybe someone will add to this.

Colour Blend
Request["txtname"] helps you get the values from the HttpContext on post.
Colour Blend
+1  A: 

It almost sounds like you're trying to use an ArrayList to store contact information for multiple people. If that is the case, you will probably want to do it a bit differently. You can create a Contact object that has members for each piece of information you want to store (e.g. firstname, lastname, phone, mobile, address1, address2, etc). Then you can just add Contact objects to your ArrayList like:

Contact contact1 = new Contact();

contact1.setFirstname("Bob");

myArrayList.add(contact1);

spork
A: 
public void addContact()
{
    ArrayList<String> details = new ArrayList<String>();
    details.add(txtname.getText());
    details.add(txtnum.getText());
    details.add(txtmob.getText());
    details.add(txtadd1.getText());        
}

Sorry I don't have an IDE open, but I think this is closer to what you are after.

Kyle