java - How to return specific int values from an array; How to make a testclass? -
i started code assignment uni, after short while, got stuck.
public class mostcommonelemnt { //private int[] liste; public mostcommonelemnt() { //int[] liste = {1,2,3,4,5,6,7,8,9}; }
is bad initialize array in constructor, or not necassary?
public int findmostcommonelemnt(int[] list) { int = 0; for(int = 0; < liste.length; i++) { = list[i]; } return help; }
here tried specific int value (or number) returned int array, "help" returns last number int array. how 2nd or 4th? see of them can use system.out.println();
public static void main (int[] args) { //int[] liste = {1,2,3,4,5,6,7,8,9}; system.out.println(new mostcommonelemnt().findmostcommonelemnt(int[] list)); } }
in section tried make testmethod but, can not work, bluej (we have use uni) complaints something. not know, after
new mostcommonelemnt().
i want, programm take specific numbers, want test. e.g: {1,2,3,4,5,6,7...} otherwise have type them in, gets boring fast.
this shorter, works.
public static int findmostcommonelement(int[] list) { int max = 0; (int : list) if (i > max) max = i; int[] newarr = new int[max]; (int = 0; < max; i++) newarr[i] = 0; (int = 0; < max; i++) newarr[list[i]] += 1; int mostcommon = 0; int mostcommonmax = 0; (int = 0; < max; i++) if (newarr[i] > mostcommonmax) { mostcommonmax = newarr[i]; mostcommon = i; } return mostcommon; }
Comments
Post a Comment