java - List.add() Class Expected -
i'm taking java class in college. instructor teacher languages derived c, can't figure out what's going on piece of code. read on page http://docs.oracle.com/javase/6/docs/api/java/util/list.html use syntax "list[].add(int index, element)" add specific objects or calculations specific indexes, reduced amount of coding needed. program i'm looking create random stat generator d&d, practice. method giving error below:
//statgenrator used actionlistener
private string statgenerator () { int finalstat; string returnstat; //creates empty list. int[] nums={}; //adds random number 1-6 each list element. (int i; > 4; i++) nums[].add(i, dice.random(6)+1); //marks 'add' "error: class expected" //sorts list decending order, drops //lowest number adding 3 highest numbers //in list. arrays.sort(nums); finalstat = nums[1] + nums[2] + nums[3]; //converts integer string set //texbox. returnstat = finalstat.tostring(); return returnstat; }
my end goal use kind of sorted list or method of removing lowest value in set. point of method generate 4 random numbers 1-6, drop lowest , add 3 highest together. final number going text of textbox, converted string , returned. remainder of code works correctly, having trouble method.
if has ideas, i'm ears. i've researched bit , found using arraylist make new list object, i'm not sure on syntax it. final note, tried looking syntax in question, couldn't find anywhere on stackoverflow. apologies if missed something, somewhere.
arrays fixed size, need allocate space slots @ start. put numbers array assign nums[i]
. no add() method needed.
int[] nums = new int[4]; (int = 0; < 4; i++) nums[i] = dice.random(6) + 1; arrays.sort(nums); finalstat = nums[1] + nums[2] + nums[3];
alternatively, if want dynamically-sized array, use arraylist. arraylist can grow , shrink.
list<integer> nums = new arraylist<integer>(); (int = 0; < 4; i++) nums.add(dice.random(6) + 1); collections.sort(nums); finalstat = nums.get(1) + nums.get(2) + nums.get(3);
notice how different syntax due arraylist being class rather built-in type.
Comments
Post a Comment