Results 1 to 4 of 4
  1. #1
    mwenchong is offline Member
    Join Date
    Nov 2010
    Posts
    14
    Rep Power
    0

    Default Arrays or ArrayLists

    I have great interest in java and I am teaching myself the language. I want to know if I can create a class the will hold an array, add elements to the array or arraylist. This class should be called in the main method.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Why not try it and see what happens? But in general if your collection will have a variable sized number of members, then use an ArrayList, and if a fixed number, then an array. Much luck.

  3. #3
    mwenchong is offline Member
    Join Date
    Nov 2010
    Posts
    14
    Rep Power
    0

    Default

    I tried something like this for the main method:
    code:
    import java.util.*;
    public class ArrayListClassDemo {
    public static void main(String args[]) {

    ArrayListClass yes = new ArrayListClass();

    yes.add("C");
    yes.add("A");

    }
    }


    and for the class i did this. I know this is totally wrong. Please guide me.

    import java.util.*;
    public class ArrayListClass {

    public ArrayListClass() {

    }

    public void makeArray() {
    // create an array list
    ArrayList al = new ArrayList();
    System.out.println("Initial size of al: " + al.size());

    }

    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    and for the class i did this. I know this is totally wrong. Please guide me.
    I can't really read your code without code tags, and I ask you to please edit your post above and use code tags. This has been requested of you before, in fact just yesterday. To see how to do this, please read the first link in my signature links below.

Similar Threads

  1. array of ArrayLists
    By imorio in forum New To Java
    Replies: 8
    Last Post: 09-24-2011, 01:05 PM
  2. ArrayLists
    By Freakzoyd in forum New To Java
    Replies: 4
    Last Post: 11-12-2010, 04:27 AM
  3. ArrayList of ArrayLists
    By coolnfunky_raj in forum New To Java
    Replies: 10
    Last Post: 07-03-2008, 10:07 AM
  4. arraylists problem
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 07:38 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
  •