Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    gretaks is offline Member
    Join Date
    Sep 2011
    Location
    Karmėlava, Lithuania
    Posts
    25
    Rep Power
    0

    Default Need help with arrays

    There are a few numbers array. Shape into a new array of numbers which are larger than the first number, the data array. Both the maximum number of blocks in the search for and compare with each other. Print the original array, formed by an array and compare the results.
    Java Code:
    package lab1;
    
    public class Klase1 {
        public void metodoParinkimas(){
         System.out.println("****** Lab1 ******");
         int[] mas1={4, 2, 5, 3, 8};
         int[] mas2= new int[10];
         System.out.print("mas1=" + java.util.Arrays.toString(mas1));
         System.out.print("\n");
         met1Masyvas1(mas1,mas2); 
         System.out.println("mas2=" + java.util.Arrays.toString(mas2));
         System.out.print("\n");
         
        }
        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];        
        }
    
        
        }
    Result:
    run:
    ****** Lab1 ******
    mas1=[4, 2, 5, 3, 8]
    mas2=[5, 8, 0, 0, 0, 0, 0, 0, 0, 0]

    BUILD SUCCESSFUL (total time: 0 seconds)
    Last edited by gretaks; 09-12-2011 at 08:20 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Need help with arrays

    Third line should be 5 8
    not 0 ten times written..
    i made a mistake somewhere..
    Where in your code do you print out the third line?
    Look at the code there and see why it prints 10 0s instead of the two numbers you want.

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

    Default Re: Need help with arrays

    int n=met1Masyvas1(mas1,mas2);
    for(int i=0;i<10;i++)
    mas2[i]=n;
    System.out.println(n);
    for(int i=0;i<10;i++)
    System.out.print(" "+mas2[i]);

    here is something wrong maybe because i made "n" int its value .. maybe it should be like int[] but then it comes with errors, can you write a correct code? i tried to correct it 2days :D

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Need help with arrays

    In the code you posted, the first loop sets all the values of the array to n and the second loop prints them out.
    What is the purpose of printing out an array that you know only contains the value n?

    What are the values in mas2 before you change them all to n? Print it out to see what the method has done to it BEFORE you change it all to n.

    Here is an easy way to print the contents of an array:
    System.out.println("mas2=" + java.util.Arrays.toString(mas2));

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

    Default Re: Need help with arrays

    thank you for help, i made everything like in C++ because i only know JAVA for 3days i think..

    ok now 10 times writing because of this i guess int[] mas2= new int[10];
    but how to create a new array not using 10? i mean 10 space for 10 values :/ i tried to use break after loop but it not helps me to write only 5 8

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Need help with arrays

    Did you try this:
    What are the values in mas2 before you change them all to n? Print it out to see what the method has done to it BEFORE you change it all to n.

    Here is an easy way to print the contents of an array:
    System.out.println("mas2=" + java.util.Arrays.toString(mas2));

    here is what is printed when I print:
    0mas1=[4, 2, 5, 3, 8]
    0 1mas2=[5, 8, 0, 0, 0, 0, 0, 0, 0, 0]
    0 2mas2=[5, 8, 0, 0, 0, 0, 0, 0, 0, 0]

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

    Default Re: Need help with arrays

    im not using n anymore so it show to me like this:
    mas1=[4, 2, 5, 3, 8]
    mas2=[5, 8, 0, 0, 0, 0, 0, 0, 0, 0]

    i don;t know what to do with that 0... :/

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Need help with arrays

    i don;t know what to do with that 0
    What do you want the program to display?
    The print outs were for debugging the code.

    You will have to change what it prints to so it is what you want to see.

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

    Default Re: Need help with arrays

    ok i need that in mas2 array will be {5, 8} (from mas1 array if other numbers are bigger than first number mas1[0], that bigger numbers you have to put in mas2 array)
    so im trying to do that :/

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Need help with arrays

    Can the method return the count of values it put into the mas2 array?

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

    Default Re: Need help with arrays

    someone said it can't..but i don;t know how to do this exercise.. i have time until wednesday

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Need help with arrays

    someone said it can't
    What have you tried?

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

    Default Re: Need help with arrays

    i was written in c++ so its easy.. but in java its a bit different, do you know how to do this exercise in java? because i think it's easy :D

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Need help with arrays

    I am doing everything here in java. I do not do c++ on this forum.

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

    Default Re: Need help with arrays

    can you try to do this exercise? its have to be easy..
    im doing something wrong in that code..
    tommorw i will try again to do something..need sleep before university..

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Need help with arrays

    Sorry, we don't do student's homework here.

    im doing something wrong in that code..
    Can you describe what the code is doing wrong
    and also describe what the code is supposed to do.

    Then figure out why the code is not doing what you want it to do.
    Try debugging your code by adding printlns to it to show the values of variables as the execution goes.

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

    Default Re: Need help with arrays

    thank you for help i will try to look at it again.

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

    Default Re: Need help with arrays

    Are you suppose to use array only? Or can you use such ArrayList?

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

    Default Re: Need help with arrays

    Solution is in here:
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Read it especially the "Copying Arrays"

    Some point outs:
    Java Code:
        met1Masyvas1(int[] masa, int[] masb) method should return the int k only and
        not masb[k] so that you can use that return value, you'll understand when you
        read the link above.
    Some suggestion (dont know the right term):
    Java Code:
        IMO mas2 should not be greater array than mas1 because mas2 is only getting or only summarizing the mas1.
        So mas2 could be like this.
        int[] mas2 = new int[mas1.length];
    By the way this is the output after changing/adding some codes.
    Java Code:
    ****** Lab1 ******
    mas1=[4, 2, 5, 3, 8]
    mas2=[5, 8, 0, 0, 0]
    mas3=[5, 8]
    Goodluck, :)
    Last edited by mine0926; 09-13-2011 at 03:24 AM.

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

    Default Re: Need help with arrays

    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");
    //----------------------------------
    i did like this so it comes: mas2=58
    i tried to use this: System.out.println("mas2=" + java.util.Arrays.toString(mas2)); but i can;t write mas2[i] in loop
    so what i have to do that array would look like this : mas2=[5, 8] ?

Page 1 of 2 12 LastLast

Similar Threads

  1. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 01:28 PM
  2. Help with arrays!
    By corrax in forum New To Java
    Replies: 8
    Last Post: 04-06-2011, 07: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, 10: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, 01:40 AM
  5. Need help with 2D arrays...
    By rrsv2 in forum New To Java
    Replies: 3
    Last Post: 11-30-2008, 03: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
  •