Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: Array

  1. #1
    Noelf21 is offline Member
    Join Date
    Dec 2009
    Posts
    12
    Rep Power
    0

    Default Array

    Hi,

    I want to create an array of names entered into a list, so i have a text box and a button so i want to enter the information from the text box into an array when the button is clicked. What would be the best way to do this

    CHeers

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Buttons use action listeners to do things (call methods, etc) when a action is performed (like clicking the button).

    If you use netbeans with the built in GUI builder, it will do this automatically for you, so all you need to do is put something like the following into your action listener method:
    Java Code:
    //...
    myArrayList.add(theTextField.getText());
    //...
    Done.

    However, if you are trying to do all this from scratch, I suggest you look up a button action listener tutorial on google, or try this one from sun: Action Listener Tutorial

  3. #3
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Yes, that if your using a arraylist
    But if you ment what you said, an array, then you can simply add
    Java Code:
    String[] theActualArray = myArrayList.toArray();

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Good point -- I used arraylist in my example because I did not want to confuse the point with keeping track of an index, max size, etc...
    I do however recommend using ArrayList since it is a dynamic array. Since this is an app that takes user input, it is likely that the number of inputs is unknown or variable. If thats the case, an ArrayList is an attractive option, especially combined with <Generics> :D

  5. #5
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    yea, tho I always use a Vector cause they take all kinds of objectTypes

  6. #6
    Noelf21 is offline Member
    Join Date
    Dec 2009
    Posts
    12
    Rep Power
    0

    Default

    the help is very much appreciated programming isn't one of my strong points but i have to do it.

    Cheers

  7. #7
    Noelf21 is offline Member
    Join Date
    Dec 2009
    Posts
    12
    Rep Power
    0

    Default

    ANother quick question how would i get it to carry the name onto the chatroom so that they are registered as that user?

  8. #8
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Ehh what you meen?
    That you have an array contaning multiple information?

    If so then you can make a class for it like this:
    Java Code:
    public class Object{
    int someRandomNumber;
    String Name;
    }
    and then do a vector like this:
    Java Code:
    Vector<Object> list = new Vector<Object>();
    and then create a new object to add:
    Java Code:
    Object Person1 = new Object();
    and change his properties:
    Java Code:
    Person1.Name = "Me!";
    Person1.someRandomNumber = 1;
    and then add him to the list:
    Java Code:
    list.add(Person1);
    Hope this is what you asked for XD
    and srry if I sinsult you by making this super easy to understand, I just feelt for typing much :P

  9. #9
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    ArrayList and Vector are both Dynamic Vectors, both support object and generics, but ArrayList is newer and based on the List interface whereas vector was retrofitted for lists later. Vector is synchronized and arraylist is not. Also, your code:
    Java Code:
    Vector<Object> list = new Vector<Object>();
    //is the same as just
    Vector list = new Vector();
    //since not specifying a generic type <String> for example is the same as <Object>
    //<Object> is implied by default.
    By using generics properly <SomeObjectType> you can avoid having to cast between data types and also can make use of java's auto boxing/unboxing features.
    Last edited by quad64bit; 12-16-2009 at 10:44 PM.

  10. #10
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    dang I knew using Object as class name was bad XD
    But what you meen vector is sync and array is not?

  11. #11
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    yeah -- I corrected that -- it was a mis-post :D I had them backwards. Otherwise, very good point! :D

  12. #12
    Noelf21 is offline Member
    Join Date
    Dec 2009
    Posts
    12
    Rep Power
    0

    Default

    Right guys sorry to sound like a dick but this is really starting to annoy me.
    Here is the code i have can you tell me where am going wrong with this it would be a great help and if you don't mind go easy on me am amateur lol.

    import javax.swing.DefaultListModel;


    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /*
    * NewUser.java
    *
    * Created on 16-Dec-2009, 15:48:46
    */

    /**
    *
    * @author NoelF21
    */
    public class NewUser extends javax.swing.JFrame {
    static DefaultListModel client = new DefaultListModel(); //Var to store ClientList
    public NewUser() {
    initComponents();

    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jButton1 = new javax.swing.JButton();
    jTextField1 = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    jButton1.setText("Join");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    jTextField1.setText("jTextField1");

    jLabel1.setText("Please Enter a Chat Name");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(34, 34, 34)
    .addComponent(jLabel1)
    .addGap(18, 18, 18)
    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(jButton1)
    .addGap(20, 20, 20))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(75, 75, 75)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(jLabel1)
    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jButton1))
    .addContainerGap(202, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:

    DefaultListModel().add(jTextField1.getText());
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new NewUser().setVisible(true);
    }

    });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration


    private Object DefaultListModel() {
    throw new UnsupportedOperationException("Not yet implemented");
    }

    }
    Last edited by Noelf21; 12-17-2009 at 12:23 AM.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Quote Originally Posted by Addez View Post
    yea, tho I always use a Vector cause they take all kinds of objectTypes
    Vector should be looked on as deprecated. Use a List of some description, and for gods sake give it a proper class to store. Lists of Objects should be looked on as a bug waiting to happen in all but the rarest of cases (usually interfaces between layers/systems).

  14. #14
    renamed is offline Member
    Join Date
    Dec 2009
    Location
    Rio de Janeiro
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Vector should be looked on as deprecated. Use a List of some description, and for gods sake give it a proper class to store. Lists of Objects should be looked on as a bug waiting to happen in all but the rarest of cases (usually interfaces between layers/systems).
    I agree!
    Lists use generic which is a good way to prevent possible bugs.
    Vectors should only be used if you're dealing with "Java ME" platform.
    Please don't laugh at my English... I'm trying my best! :)

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Don't know the ins and outs of ME. Doesn't it have Lists then?
    Vector uses generics...it's more a case of it not originally being a part of the superior Collections set up, and later being refactored into it. It just doesn't make sense to use it anymore (never mind the synch overhead when it's often not needed).

  16. #16
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Okej.. So if Vectors can mess data up, then I can start using ArrayLists instead, they work they same, got some methods so np.
    But If I remember right, there is diffrent kinds of lists?
    Why? Since arraylist can store any objects..

    And for noa, I can debug it for you but I need the DefaultListModel package.
    If you dont got one thats why..
    Look at line 16 or 17 and ull see

  17. #17
    renamed is offline Member
    Join Date
    Dec 2009
    Location
    Rio de Janeiro
    Posts
    38
    Rep Power
    0

    Default

    Java Works with two types of lists: ArrayList and LinkedList.

    Both can store any type of data. Though the way they do it is different.

    If you are not sure which one you might use. You can easily switch your list with no problems. Both implement the interface list.

    If you want to create a list of names. You can do the following:

    Java Code:
    List<String> lstName = new ArrayList<String>();
    If you need to change the list you have chosen for any reason in the future, you just need to change one part of the code.

    Java Code:
    List<String> lstName = new LinkedList<String>();
    The names of the methods that you use to insert, remove, get and search are the same in both lists so you don't need to worry about that.

    Find below the complexity of some operations in ArrayList and LinkedList.

    Indexing
    LinkedList: O(n)
    Dynamic ArrayList: O(1)


    Insertion/deletion at end
    LinkedList: O(1)
    Dynamic ArrayList: O(1)

    Insertion/deletion in middle
    LinkedList: O(1)
    Dynamic ArrayList: O(n)

    Wasted Space (average)
    LinkedList: O(n)
    Dynamic ArrayList: O(n)

    (Taken from Wikipedia)
    Please don't laugh at my English... I'm trying my best! :)

  18. #18
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by Tolls View Post
    Don't know the ins and outs of ME. Doesn't it have Lists then?
    No it doesn't (last time I used it). That and the fact that some Swing methods/constructors are coded to the Vector implementation mean there are uses...

    But those uses don't really explain the popularity of this class. Perhaps a "List" is a really common place thing - the sort of thing that you might take to the supermarket so you don't forget the milk. While "Vector" is a mathematical sophisticate.

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Quote Originally Posted by pbrockway2 View Post
    No it doesn't (last time I used it).
    Ah right. I suppose it's down to trying to keep the ME api small(er).

    Quote Originally Posted by pbrockway2 View Post
    That and the fact that some Swing methods/constructors are coded to the Vector implementation mean there are uses...

    But those uses don't really explain the popularity of this class. Perhaps a "List" is a really common place thing - the sort of thing that you might take to the supermarket so you don't forget the milk. While "Vector" is a mathematical sophisticate.
    I know people who have come from C backgrounds who use it (I did in fact, in the early days of using Java) because of the match with stdlib, but I am surprised people don't get pulled up on using it more often, frankly.

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by renamed View Post
    Wasted Space (average)
    LinkedList: O(n)

    (Taken from Wikipedia)
    I don't believe that; for an ArrayList there are about 3/2*n-n spare slots available but a LinkedList doesn't need that. What's the actual url of that page?

    kind regards,

    Jos

Page 1 of 2 12 LastLast

Similar Threads

  1. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM
  2. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  3. How to transfer 1D array in JAVA to 3D array in C
    By fishwater00 in forum New To Java
    Replies: 0
    Last Post: 07-31-2009, 06:24 PM
  4. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  5. String array to byte array?!
    By Joe2003 in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 06:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •