• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method with two integer arrays problem  RSS feed

 
jun jie
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys!

I am currently doing some learning in java and really appreciate all the advice i can get.

My problem is that currently my code works if my method has the same array length or with a difference of one but when the array of the argument goes more than 2, i get an array out of bound exception.

Any advice on this?

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain in words what you want to happen?

Suppose you have these arrays:


What should the answer be and why?
 
jun jie
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! it should get something like below where the first element of array will multiple each other, the rest of the element will be replaced with 1 for each missing element

 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi jun,
Welcome to CodeRanch!!
the exception is occuring because the way you have designed the multipler(change this name to "multiply" as name of the method should be a verb) is dealing with arrays(to be multiplied) having a length difference of 1.
at the very first properly indent your code and make use of names(nouns for variable,class and verb for methods) which should simply reveal its use.
now in your code get your multipled array(accordingly with the array having minimum length)

now make use of some utility methods "copyOf(int[],int newLength)",new length should be the max of the lenghth of the 2 array.now do some work by yourself,their is a copyOfRange methods in Arrays use your skills to append the array returned by "copyOfRange" to the previous array returned by copyOf.

Praveen.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jun jie wrote:
Please tell your reasoning of choosing l1 and l2 as variable names? There is l3 it seems too. Also I found array3 variable. What has happened with array1 and array2 ? Doesn't your code look cryptic to yourself?
 
jun jie
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Liutauras,

Yes ive realized that i could have chosen a more suitable name for my variables.. but i cant seem to find the edit button to edit my post.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jun jie wrote:Hi Liutauras,

Yes ive realized that i could have chosen a more suitable name for my variables.. but i cant seem to find the edit button to edit my post.
Oh, I see.. No worries
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given what you have on lines 5 & 6, the comments you wrote on lines 13 & 19 are wrong. You might want to rethink the assumptions you had in writing those loops.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!