Relative Content

Tag Archive for javaarraylist

What is a fast way to count how many times a given value is present in an ArrayList?

ArrayList<Integer> myArrayList = new ArrayList<>; for (int i = 0; i < 5; i++) { myArrayList.add(i); } myArrayList.add(2); myArrayList.add(2); myArrayList.add(3); //arryaylist contains: (0,1,2,3,4,2,2,3) I want to give input to a method, and for it to return amount of times the input is seen in the array. For example: public int someMethod(int searchVal) { //code //returns […]

What is a fast way to count how many times a given value is present in an ArrayList?

ArrayList<Integer> myArrayList = new ArrayList<>; for (int i = 0; i < 5; i++) { myArrayList.add(i); } myArrayList.add(2); myArrayList.add(2); myArrayList.add(3); //arryaylist contains: (0,1,2,3,4,2,2,3) I want to give input to a method, and for it to return amount of times the input is seen in the array. For example: public int someMethod(int searchVal) { //code //returns […]

Alternatives to ArrayList for primitive types in Java for numerical computation

I am currently working on numerical computations in a specific research domain and have inherited a Java package implemented long time ago by my team. My task now is to optimize its performance. The package makes extensive use of ArrayList<Double>. As far as I know, Java, at least up to version 9 which I am using, does not support ArrayList of primitive types due to type erasure in Java generics.

In Java, what is a fast way to count how many times given value is represented in ArrayList?

ArrayList<Integer> myArrayList = new ArrayList<>; for (int i = 0; i < 5; i++) {myArrayList.add(i);} myArrayList.add(2); myArrayList.add(2); myArrayList.add(3); //arryalist contains: (0,1,2,3,4,2,2,3) I want to give input to a method, and for it to return amount of times the input is seen in the array. For example: public int someMethod(int searchVal) { //code //returns int } […]

How to sort an ArrayList not alphabetically or numerically in java

I am programming a mastermind game in java.I have an ArrayList called “checked” and as I check through the users guess, the words “black”, “white” and “blank” are added to the arrayList. Is there anyway for me to sort the checked array at the end in order of importance, e.g. first “black”, then “white”, and then “blank”?