Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calculating differences between adjacent items in an ArrayList

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey only been learning code for a few weeks and I am stuck trying to complete a method.
I am to return a new ArrayList holding the differences between adjacent items from a previous ArrayList.
etc, <1,4,5,8> returns <3,1,3>

'public ArrayList<Double> differences(ArrayList<Double> seq)' is the Method.

Can anyone give any tips at all to how I should go about this, really hit a wall.
 
Marshal
Posts: 79531
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I would start by writing down what you want to do, and then later, how you are going to do it. Don't use any words the same as programming constructs if possible. Keep going through your algorithm until you have it down to words of one syllable (well, I might let you use “number”, which has two syllables), at which point you will have instructions for writing the code.
 
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One can use a programming flowchart and that involves developing the logic using flowchart symbols. These diagrams were very useful in my earlier programming days.
 
Marshal
Posts: 8884
638
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch again

Don't forget to think about the slightly unusual cases. For instance:

  • what to do if there are no elements in a list
  • what if there is only one element in a list
  • how calculate if the two adjacent elements are -4 and -4
  • how calculate if the two adjacent elements are 4 and -4
  • how calculate if the two adjacent elements are -4 and 4

  • Probably there are more cases to think about.
     
    Greenhorn
    Posts: 11
    1
    Eclipse IDE Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Originally, I included the working code but I've been advised to edit my post to give you more guidance instead of just spoon feeding you the answer.

    Here's what I suggest for the method implementation:

    Create a new ArrayList to place the calculated differences between adjacent list elements and then iterate over the passed in list to calculate all the differences of adjacent elements.

    To calculate the difference for adjacent elements, have the method look at the current list element and the element ahead. You'll have to be careful to set your list iterator to not go to the end of the list - otherwise, you'll get an IndexOutOfBoundsException since you're always looking one step ahead of the current element.
     
    Daniel Baker
    Greenhorn
    Posts: 11
    1
    Eclipse IDE Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I realize that my previous post provided you with an answer instead of helping you "discover the answer for yourself" - I'm new here myself so didn't realize the intent of the forum when I posted my previous answer. If my previous post gets pulled, then I'll update this to provide more "guidance" instead of an answer.
     
    Campbell Ritchie
    Marshal
    Posts: 79531
    379
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Please update the previous post to show only guidance.
    Why are you using Double.valueOf() instead of simple boxing?
     
    Daniel Baker
    Greenhorn
    Posts: 11
    1
    Eclipse IDE Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Updated previous post to show only guidance instead of spoon fed code sample.

    And thanks for that boxing comment, Campbell. I totally should've used that instead of Double.valueOf().
     
    Campbell Ritchie
    Marshal
    Posts: 79531
    379
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Daniel Baker wrote:Updated previous post . . . And thanks for that boxing comment, Campbell. . . . .

     
    Look! It's Leonardo da Vinci! And he brought a tiny ad!
    We need your help - Coderanch server fundraiser
    https://coderanch.com/t/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic