arraylist

Select value from arraylist based on its percentage [duplicate]

北城以北 提交于 2020-07-07 15:13:30
问题 This question already has answers here : How to pick an item by its probability? (12 answers) Closed 4 years ago . I got an arraylist of objects. Each objects got 2 values, content and weight . content is just a simple string while weight decides the percentage of how often the string should be used. If I have 2 objects in the array: Object 1: content: hello weight: 20 Object 2: content: hey weight: 80 First objects content should be used 20% of times while the second objects content should

Why need main method in order to use arraylist methods in the class?

女生的网名这么多〃 提交于 2020-07-05 11:59:05
问题 I can do this: import java.util.ArrayList; public class Array { public static void main(String args[]){ ArrayList<String> myList = new ArrayList<String>(); myList.add("S"); } } However I CANNOT do this: import java.util.ArrayList; public class Array { ArrayList<String> myList = new ArrayList<String>(); myList.add("S"); } Why I have to include the main method? 回答1: Because Java classes consist of methods and blocks. You can't have a raw statement like myList.add("S"); Finally, your application

Cannot apply indexing with [] to an expression of type `object'

安稳与你 提交于 2020-07-05 06:26:10
问题 H ere is my code: An ArrayList of ArrayList that returns a float: public ArrayList walls=new ArrayList(); public void Start() { walls[0] = ReturnInArrayList(279,275,0,0,90); walls[1] = ReturnInArrayList(62,275,0,0,0); walls[2] = ReturnInArrayList(62,275,62,0,90); walls[3] = ReturnInArrayList(217,275,62,-62,0); walls[4] = ReturnInArrayList(62,275,279,0,90); walls[5] = ReturnInArrayList(41,275,279,0,0); walls[6] = ReturnInArrayList(279,275,320,0,9); walls[7] = ReturnInArrayList(320,275,0,-279,0

Cannot apply indexing with [] to an expression of type `object'

陌路散爱 提交于 2020-07-05 06:24:49
问题 H ere is my code: An ArrayList of ArrayList that returns a float: public ArrayList walls=new ArrayList(); public void Start() { walls[0] = ReturnInArrayList(279,275,0,0,90); walls[1] = ReturnInArrayList(62,275,0,0,0); walls[2] = ReturnInArrayList(62,275,62,0,90); walls[3] = ReturnInArrayList(217,275,62,-62,0); walls[4] = ReturnInArrayList(62,275,279,0,90); walls[5] = ReturnInArrayList(41,275,279,0,0); walls[6] = ReturnInArrayList(279,275,320,0,9); walls[7] = ReturnInArrayList(320,275,0,-279,0

Add String Array to ArrayList [duplicate]

喜欢而已 提交于 2020-07-02 07:13:08
问题 This question already has answers here : How to add elements of a string array to a string array list? (11 answers) Closed 4 years ago . I have 2 String Arrays String[] question1 = {"Q1","Q2","Q3","Q4"}; String[] question2 = {"Q5","Q6","Q7","Q8"}; And one ArrayList ArrayList<String> aList = new ArrayList<String>(); How can I manipulate them if i want to access to a member of ArrayList . I tried converting both arrays to String but it doesn't give solution. 回答1: ArrayList<String> aList = new

Get Array of object From Firestore

萝らか妹 提交于 2020-06-29 06:23:47
问题 I getting data from firestore put it in to ArrayList arraylist=document.get("questionsList") as ArrayList<Question> Toast.makeText(context, arraylist.size, Toast.LENGTH_LONG).show() and its ok when I print the size of Array put when I need to get Question Item from Arraylist Toast.makeText(context, arraylist!![0].question, Toast.LENGTH_LONG).show() the result is java.util.HashMap cannot be cast to Question document in firestore image here Question class class Question (var question:String,var

Choosing a Random word from a text file

北城以北 提交于 2020-06-29 04:12:08
问题 I'm trying to develop a hangman as an assignment, and is unable to get one random word from a Text file(which has various words and each word is separated with a space). I've written a code to get a random word, but unable to pick one words and replace it, with the sample string (String w = "this";) i have in the "Function()". public String randomWord(String wordran) { try { BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\Admin\\Documents\\NetBeansProjects\\Main\\words

How to work with Java ArrayList?

房东的猫 提交于 2020-06-27 04:27:08
问题 Please, I try to add item to arrayList like example below: ArrayList<Integer> list = new ArrayList<>(); list.add(2); list.add(5); list.add(7); for(int i : list ){ if((i%2) == 0){ list.add(i*i); } } but it throws an exception java.util.ConcurrentModificationException Could you please advice how can I add item like this or what kind of list (container) is to be used correctly? 回答1: Use a regular for loop. Enhanced for loops do not allow you to modify the list (add/remove) while iterating over

How to work with Java ArrayList?

被刻印的时光 ゝ 提交于 2020-06-27 04:26:10
问题 Please, I try to add item to arrayList like example below: ArrayList<Integer> list = new ArrayList<>(); list.add(2); list.add(5); list.add(7); for(int i : list ){ if((i%2) == 0){ list.add(i*i); } } but it throws an exception java.util.ConcurrentModificationException Could you please advice how can I add item like this or what kind of list (container) is to be used correctly? 回答1: Use a regular for loop. Enhanced for loops do not allow you to modify the list (add/remove) while iterating over

How to loop text of an element in JLabel?

老子叫甜甜 提交于 2020-06-27 04:07:16
问题 I'm having a problem with loops. How does this loop, loop through different values ​​and display all values ​​on the label, rather than displaying the last value of the array on the label? Image of code for loop. 回答1: This below code is just copied(written) from your screenshot. which has the minor bug. sinhvien sv = new sinhvien(); sv.setdata("CC",12); sv.setdata("CL",14); sv.setdata("CCCL",16); s1.add(sv); As you have only created one instance of sv and setting the value 3 times. Value CCCL