Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25
  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Need help with arrays

    what i have to do that array would look like this : mas2=[5, 8] ?
    Create an array with 2 elements.

  2. #22
    gretaks is offline Member
    Join Date
    Sep 2011
    Location
    Karmėlava, Lithuania
    Posts
    25
    Rep Power
    0

    Default Re: Need help with arrays

    of course ;D i need to forget C++ :D
    thank you

  3. #23
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Need help with arrays

    Did you read the link I gave? Especially the topic "Copying Arrays" ? As I have said, the answer is there.

    I suggest that met1Masyvas1() method should return integer 'k' only and not 'masb[k]'. The reason why I tell you to return only integer 'k' is because it is the maximum index that has been filled-up in masB.

    Here is your code, I commented your original code and put what I am suggesting you.
    Java Code:
        public static int met1Masyvas1(int[] masa, int[] masb){
            int k=0;
            for(int i=0;i<masa.length;i++)
                if(masa[0]<masa[i]){
                    masb[k]=masa[i];
                    k++;
                }
            //return masb[k];
            return k;
        }

  4. #24
    gretaks is offline Member
    Join Date
    Sep 2011
    Location
    Karmėlava, Lithuania
    Posts
    25
    Rep Power
    0

    Default Re: Need help with arrays

    of course i did what you said and i red that article
    Java Code:
        public static int met1Masyvas1(int[] masa, int[] masb){
            int k=0;
            for(int i=0;i<masa.length;i++)
                if(masa[0]<masa[i]){
                    masb[k]=masa[i];
                    k++;
                }
            return k;        
        }
    but then i use this one:
    Java Code:
         int n=met1Masyvas1(mas1,mas2);
         System.out.print("mas2=");
         for(int i=0;i<n;i++){
             System.out.print(mas2[i]);
         }
         System.out.print("\n");
    it comes like this: mas2=58

    so use this:
    System.out.println("mas2=" + java.util.Arrays.toString(mas2));
    System.out.print("\n");
    because i changed to 2 int[] mas2= new int[2];

  5. #25
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Need help with arrays

    In the link I gave there is
    Java Code:
        System.arraycopy(source, start at index, destination, start of index, end of index)
    you can use arrayCopy() method to copy mas2 to mas3 with only specified index (which is your integer k).

    And the print mas3 just like how you print mas1 and mas2

Page 2 of 2 FirstFirst 12

Similar Threads

  1. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 02:28 PM
  2. Help with arrays!
    By corrax in forum New To Java
    Replies: 8
    Last Post: 04-06-2011, 08:36 AM
  3. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 11:32 AM
  4. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  5. Need help with 2D arrays...
    By rrsv2 in forum New To Java
    Replies: 3
    Last Post: 11-30-2008, 04:15 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
  •