• Post Reply Bookmark Topic Watch Topic
  • New Topic

I need help please with ArrayLists  RSS feed

 
Obiora Nnene
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, please I am quite new to java programming and I need someone to show me where I have gone wrong in this code. The objective is to return the rectangle object with the smallest area and return null if the ArrayList is empty from an ArrayList of Rectangles. My code runs but does not work well for all test cases. Thank you.

 
Ryan Tracy
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a couple of problems you have going. One is that temp never changes and I could argue temp is not even needed. You only need to test between two rectangles yet you have three variables smallestArea, temp, and smallest. What you need is one variable to hold the currentSmallestRectangle and one variable to hold the nextRectangle for testing against. Once nextRectangle is smaller than currentSmallestRectangle then you need to set currentSmallestRectangle equal to nextRectangle and then continue on in your loop. Also, currentSmallestRectangle needs to be initialized to the 0 index outside of your loop.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Obiora Nnene
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ryan i have followed your instructions and its works better just that for a null test case it gives an indexOutOfBounds error. Here is what i have done.
 
Obiora Nnene
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Ryan i have followed your instructions and its works better just that for a null test case it gives an indexOutOfBounds error. Here is what i have done.

public Rectangle smallestArea()
{

Rectangle currentsmallestRectangle = list.get(0);

for (int i = 1; i < list.size(); i++)
{

Rectangle nextRectangle = list.get(i);

if((nextRectangle.getWidth() * nextRectangle.getHeight()) < (currentsmallestRectangle.getWidth() * currentsmallestRectangle.getHeight()))
{
currentsmallestRectangle = nextRectangle;
}

if (list.isEmpty())
{
currentsmallestRectangle = null;
}

}
return currentsmallestRectangle;
}

 
Ryan Tracy
Ranch Hand
Posts: 47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple more problems you have with this is you call list.isEmpty() inside the very loop that has just looped because it had a Rectangle in it. Try and rethink that one. Also, if your list is really empty what will happen when list.get(0) is called? You might want to do a check before you get to that call.
 
Obiora Nnene
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tips Ryan. I have gotten the code to work properly now
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!