• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compare the values in 2 array.  RSS feed

 
timothy Lang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, im newbie in Java. Here are the 2 arrays:



1) how to find biggest value in between both arrays? it looks like this {6,13,6,15,5}.

Sorry for my bad English.
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haven't you tried anything ?

I will just give you the steps

1) Use a for-loop with the initialization of counter-variable from 0 to length of array minus 1



2) Use if-statement to compare the values so that you can find whether array 1 elements are greater than the array 2 elements. If yes, store the greater value of array 1 in third array. If no,
in else-statement you can store array 2 elements in third array.

NOTE: There are many ways of finding the greater values, but the above was the method which came in my mind according to your question.
and

Welcome to Ranch

 
timothy Lang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, thank you for reply. Here the code im doing so far:




--------------------------------------------


1) Any corrections to store greater number in another array ?

Thanks :-D

 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
timothy Lang wrote: it looks like this {6,13,6,15,5}.



Firstly i thought you want an array with all maximum values filled in it


Then you wrote
biggest value


So i mean only a single value which is maximum from both arrays



As in your case both the arrays are of same type it becomes much easier.

Note in your code you have written the condition in for-loop as it should not be minus one and only

There can be many better ways of doing the above code.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think what you are doing wrong is trying to code when you don’t yet know what to code. The vauge thread title suggests this too.
You need to write down on paper, using a soft pencil and an eraser (the latter is the most important piece of hardware at present ), exactly what you intend to achieve, and how you intend to achieve it. Draw diagrams of the arrays and how you intend to copy the larger values into another array. Once you have done that, it will become obvioius how to copy the array across.

By the way: Have you come across the Math.max() method? You might be able to use that, if the assignment permits it.   If you are feeling more ambitious, find the file called src.zip, which resides in your Java™ installation folder. Unzip that and find the Math class file (its full name is java.lang.Math, so you will find it in the folder called lang inside the folder called java). Find the max method and see how that is implemented. That might give you a hint for a neat and succinct way to choose the larger value to insert into your new array.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rameshwar Soni wrote: . . . There can be many better ways of doing the above code.
Even so, you ought to read what it says on this forum’s title page:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
Giving a complete solution usually does not help anybody learn, but does more harm than good. I have therefore pulled rank (please don’t be annoyed with me) and deleted part of your post.
Please don’t go giving complete solutions like that.

Both of you: The indentation is inconsistent; have a look at these suggestions, and make sure not to use tabs for indenting. I have added code tags and you can see how much better the code looks now
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!