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 2 of 2

Thread: Create new Array from old Array

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Create new Array from old Array

    Hey,


    I have a String array with multiple elements in it and I want to create another String array from it but, I don't want to take all the elements from the first array, theres a criteria for it the strings length.


    So Ive got an array with 10 elements in..
    [1] = Eitens
    [2] = Dietrumpream
    [3] = Formal
    [4] = and
    [5] = Weide
    [6] = Forma
    [7] = Spreorphotes
    [8] = the
    [9] = Bly
    [10] = Coming

    and the new array will have the following conditions, each element cant be longer than 3 characters long, so the second array should be of size 3 with the elements "and","the","bly" and of course in the same order as they were?


    This is what I've to so far:
    int number = 0;
     
    for (int i = 0; i < S1.length; i++) 
        {
            if (S1[i].length() >= key.length())
            {
                 number++;
             }
        }
     
        String sorted[] = new String[number];
        int j = 0;
        for(int i = 0; i<S1.length;i++)
       {
            if (S1[i].length() <= key.length()){
                sorted[j] = S1[i];
                j++;
            }
        }


    Where am I going wrong?
    Last edited by helloworld922; February 24th, 2010 at 12:37 PM.


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

    Default Re: Create new Array from old Array

    You're putting the elements into the exact same spot as they were in the original array. What you should be doing is using a separate counter to determine where to put the elements. This won't change the fact that you're array will have a bigger size still. There are two possible solutions: use an ArrayList, which will keep track of the size for you, or re-allocate a third array to the number of elements you want to keep after you've found all of them and transfer them over.

Similar Threads

  1. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM
  2. 2d Array
    By mgutierrez19 in forum Collections and Generics
    Replies: 1
    Last Post: October 24th, 2009, 10:48 PM
  3. 2D array help plz
    By Kilowog in forum Collections and Generics
    Replies: 2
    Last Post: October 11th, 2009, 03:23 PM
  4. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM
  5. Program of an Array in Java
    By eric_kapre in forum Collections and Generics
    Replies: 1
    Last Post: February 5th, 2009, 06:30 AM