This week's giveaway is in the Java in General forum.
We're giving away four copies of Java Challengers and have Rafael del Nero on-line!
See this thread for details.
Win a copy of Java Challengers this week in the Java in General forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

maxArray method

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The instruction that was given to me was maxArray will be used as:
Keeps track of every occurrence of the max number in the given array.
* Returns an ArrayList that contains every occurrence of the max number.
* Uses the findMax(int[] array) method.
* If the array is empty, returns null.

What I've got so far is this.


Main


The problem is at the method I have, when I return max it gives me int cannot be converted to ArrayList<Integer> and I this is the only idea that I've come up. Need help please

Source code of the task I'm doing:
Pastebin
 
Marshal
Posts: 72657
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jiggy Palconit wrote:. . .
* Returns an ArrayList that contains every occurrence of the max number.

Surely that should read List. There is something missing from these instructions; if there are three occurrences of the largest number, 23, the List will simply read [23, 23, 23].


* Uses the findMax(int[] array) method.

That is a good idea; separating methods makes for better code.


* If the array is empty, returns null.

You will have to return null then, but you should know that is bad design; It is far better to return a 0‑element List. You might do well to query that with your teacher.

. . .
. . .

I think you will have to start again. I don't think that code can be rescued. Start by writing down on paper in tiny words what you want that method to do. The method isn't doing one thing; it is doing two things, finding the maximum in the array and then (incorrectly) copying that number into the List. Make each method do one thing, and your code will be better. You already know how to find the maximum. Now you want a method to find how often that number occurs in the array and copy each into the List.
 
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic