Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to dynamically create ArrayLists (or something similar)?

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    18
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Unhappy How to dynamically create ArrayLists (or something similar)?

    Hi,
    I have an algorithm that using cluster centroids, searches through an array of doubles (see below) putting the values closest to the centroids in repective arrayLists.
    For example (handtyped, not source code, because it works)

    PHP Code:
    double[] array {1,2,3,4,5,6,7,8,9,10}
    double[] centroids {1,4,7
    Using Euclidean distance this creates the following three ArrayLists

    PHP Code:
    list1{1,2}
    list2{3,4,5}
    list3{6,7,8,9,10
    This all works fine, however could someone please help me to dynamically create arrayLists? Right now I'm actually instantiating three of them by hand, but in the working program the number of centroids will be dynamic so the program will need to create as many as required. I was thinking a 2D array, but not really sure if that is the best way to go about this.

    I'm really stuck on this, any help would be hugely appreciated


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: How to dynamically create ArrayLists (or something similar)?

    Hello,

    How about using an actual ArrayList rather than an array?

        final List<Double> doubleArray = new ArrayList<Double>();
        doubleArray.add(1.0);
        doubleArray.add(2.0);
        doubleArray.add(3.0);
        doubleArray.add(4.0);
        doubleArray.add(5.0);
        doubleArray.add(6.0);
        doubleArray.add(7.0);
        doubleArray.add(8.0);
        doubleArray.add(9.0);
        doubleArray.add(10.0);

    Something like that, and you can just go on and on and on because arraylists grow while you add things to them

    EnjoY!

    // Json

  3. The Following 2 Users Say Thank You to Json For This Useful Post:

    igniteflow (August 6th, 2009), JavaPF (August 6th, 2009)

  4. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    18
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default Re: How to dynamically create ArrayLists (or something similar)?

    Thanks for the reply Json.
    I am already using multiple ArrayLists, but my problem is that I want the program to create as many as it requires dynamically. I need to keep the clusters separate, so I don't want to add them all to a single ArrayList, but rather create the same number of ArrayLists as the number of clusters (which is specified by the user). Hope that makes sense.

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to dynamically create ArrayLists (or something similar)?

    Create an array list of array lists
    ArrayList<ArrayList<Double>> clusters = new ArrayList<ArrayList<Double>>();

    Then, each time you want a new cluster do:
    ArrayList<Double> set = new ArrayList<Double>();
    // add stuff to set
    set.add(5);
    ...
    clusters.add(set);

  6. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    igniteflow (August 6th, 2009), JavaPF (August 6th, 2009)

  7. #5
    Junior Member
    Join Date
    Jun 2009
    Posts
    18
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default Re: How to dynamically create ArrayLists (or something similar)?

    Awesome, thanks helloworld922!!

Similar Threads

  1. How to Create a server socket to listen for incoming connections?
    By JavaPF in forum Java Networking Tutorials
    Replies: 3
    Last Post: October 28th, 2011, 09:02 AM
  2. Java error "could not create java virtual machine"
    By aubrey4444 in forum Java Theory & Questions
    Replies: 17
    Last Post: October 3rd, 2010, 12:51 PM
  3. Add Jmol applet dynimically in JSF(java server faces)
    By megha in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: May 15th, 2009, 06:16 AM
  4. Replies: 0
    Last Post: February 3rd, 2009, 01:15 AM