# Need help with arrays

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-12-2011, 06:46 PM
gretaks
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.
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)
• 09-12-2011, 07:04 PM
Norm
Re: Need help with arrays
Quote:

Third line should be 5 8
not 0 ten times written..
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.
• 09-12-2011, 07:50 PM
gretaks
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
• 09-12-2011, 07:54 PM
Norm
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));
• 09-12-2011, 08:23 PM
gretaks
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
• 09-12-2011, 08:31 PM
Norm
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:
Quote:

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]
• 09-12-2011, 08:44 PM
gretaks
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... :/
• 09-12-2011, 08:50 PM
Norm
Re: Need help with arrays
Quote:

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.
• 09-12-2011, 08:56 PM
gretaks
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 :/
• 09-12-2011, 08:57 PM
Norm
Re: Need help with arrays
Can the method return the count of values it put into the mas2 array?
• 09-12-2011, 09:09 PM
gretaks
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
• 09-12-2011, 09:10 PM
Norm
Re: Need help with arrays
Quote:

someone said it can't
What have you tried?
• 09-12-2011, 09:15 PM
gretaks
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
• 09-12-2011, 09:20 PM
Norm
Re: Need help with arrays
I am doing everything here in java. I do not do c++ on this forum.
• 09-12-2011, 09:23 PM
gretaks
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..
• 09-12-2011, 09:26 PM
Norm
Re: Need help with arrays
Sorry, we don't do student's homework here.

Quote:

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.
• 09-12-2011, 09:29 PM
gretaks
Re: Need help with arrays
thank you for help i will try to look at it again.
• 09-13-2011, 02:48 AM
mine0926
Re: Need help with arrays
Are you suppose to use array only? Or can you use such ArrayList?
• 09-13-2011, 03:18 AM
mine0926
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:
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

Some suggestion (dont know the right term):
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.
Code:

****** Lab1 ******
mas1=[4, 2, 5, 3, 8]
mas2=[5, 8, 0, 0, 0]
mas3=[5, 8]

Goodluck, :)
• 09-13-2011, 03:06 PM
gretaks
Re: Need help with arrays
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] ?
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last