arraylist

Unable to add a String to an ArrayList: “misplaced construct(s)”

左心房为你撑大大i 提交于 2019-12-19 10:14:19
问题 I am trying to add a string to an ArrayList in Java, but I can't seem to get it to work. Currently, I have the following code: List food_names = new ArrayList(); food_names.add("pizza"); Why do I get these errors: Syntax error on token ""pizza"", delete this token Syntax error on token(s), misplaced construct(s) 回答1: You have to use food_names.add("pizza") in function, for example: public void AddFood() { food_names.add("pizza"); } Hope helps 回答2: why don't you use List Generics List

Why does ArrayList not throw ConcurrentModificationException when modified from multiple threads?

一笑奈何 提交于 2019-12-19 09:23:43
问题 ConcurrentModificationException : This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible. Above is ConcurrentModificationException definition from javadoc. So I try to test below code: final List<String> tickets = new ArrayList<String>(100000); for (int i = 0; i < 100000; i++) { tickets.add("ticket NO," + i); } for (int i = 0; i < 10; i++) { Thread salethread = new Thread() { public void run() { while (tickets

Read from a database in android to an arraylist

别等时光非礼了梦想. 提交于 2019-12-19 09:10:25
问题 I'm very new to android development and can't figure out how to read from my database/table into an arrayList. Right now I have: Cursor c = myDB.rawQuery("SELECT * FROM " + TableName , null); int Column1 = c.getColumnIndex("Field1"); int Column2 = c.getColumnIndex("Field2"); // Check if our result was valid. c.moveToFirst(); if (c != null) { // Loop through all Results do { String Name = c.getString(Column1); int Age = c.getInt(Column2); Data =Data +Name+"/"+Age+"\n"; }while(c.moveToNext());

Java, getting an ArrayList index by object

[亡魂溺海] 提交于 2019-12-19 08:51:46
问题 so I have hit a little problem in my code I have: synchronized(clients) clients.remove(this); } for when a client disconnects, but now I need to be able to send the name of that client to all the other clients, and to do this I essentialy need to do something like synchronized(clients) broadcast("Remove:"+clients.get(this).name); clients.remove(this); } but obviously I can't get an index with the "this", so how do I go about getting the right clients name? Thanks! 回答1: why don't you simply

2D dynamic array using ArrayList in Java

↘锁芯ラ 提交于 2019-12-19 08:20:11
问题 I need to implement a 2D dynamic array. The number of rows is fixed, say n. But the number of columns for each row is not fixed and equivalent. For instance, the first row has 3 elements and the second row has 5 elements. How to do this in Java using Arraylist. Thanks. 回答1: How about List<List<Foo>> ? For Example: List<List<Foo>> list = new ArrayList<List<Foo>>(); List<Foo> row1 = new ArrayList<Foo>(); row1.add(new Foo()); row1.add(new Foo()); row1.add(new Foo()); list.add(row1); List<Foo>

deserialize json string with gson

徘徊边缘 提交于 2019-12-19 07:35:26
问题 My java servlet returns a json string in this way: Gson gson = new Gson(); String lista = gson.toJson(utenti); System.out.println(lista); request.setAttribute("lista", lista); request.getRequestDispatcher("GestioneUtenti.jsp").forward(request, response); now, in the jsp page I want to have my arrayList again. I try to do this: <% String lista = (String)request.getAttribute("lista"); Gson gson = new Gson(); ArrayList<Utente> users = gson.fromJson(lista, TypeToken.get(new ArrayList<Utente>()

RecyclerView

时光毁灭记忆、已成空白 提交于 2019-12-19 07:26:10
关于布局 1.添加依赖 2.添加<android.support.v7.widget.RecyclerView></android.support.v7.widget.RecyclerView> 关于Activity 1.初始化RecyclerView 2.创建RecyclerView适配器 1)继承RecyclerView.Adapter<MyViewHolder>泛型为自定义内部类ViewHolder 2)创建内部类ViewHolder 继承于RecyclerView.ViewHolder 在这里自定义布局加载控件 class MyViewHolder extends RecyclerView.ViewHolder{ public final TextView tv; public MyViewHolder(View itemView) { super(itemView); tv = (TextView)itemView.findViewById(R.id.tv); } 3)构造方法中存入数据与上下文对象 4)重写三个方法 ①创建RecyclerView的布局并返回viewHolder LayoutInflater mLayoutInflater=LayoutInflater.from(mContent); View inflate = mLayoutInflater

Adding Multiple Values in ArrayList at a single index

风格不统一 提交于 2019-12-19 06:29:09
问题 I have a double ArrayList in java like this. List<double[]> values = new ArrayList<double[]>(2); Now what I want to do is to add 5 values in zero index of list and 5 values in index one through looping. The zeroth index would have values {100,100,100,100,100} The index 1 would have values {50,35,25,45,65} and all of these values are stored in a double array in following order double[] values = {100,50,100,35,100,25,100,45,100,65} How can i do it? 回答1: @Ahamed has a point, but if you're

Working with an ArrayList of Functions in Java-8

大憨熊 提交于 2019-12-19 06:23:27
问题 Problem Description: I want to be able to use an ArrayList of Functions passed in from another class (where the Functions have been defined in that other class). If the list of Functions, which may have different input and return types, are defined in one class, I want to be able to pass an ArrayList of some of them, with possible duplicates, as a parameter to some other class's constructor or method and perform operations using them. Code Description: The code below is a greatly simplified

Working with an ArrayList of Functions in Java-8

空扰寡人 提交于 2019-12-19 06:21:59
问题 Problem Description: I want to be able to use an ArrayList of Functions passed in from another class (where the Functions have been defined in that other class). If the list of Functions, which may have different input and return types, are defined in one class, I want to be able to pass an ArrayList of some of them, with possible duplicates, as a parameter to some other class's constructor or method and perform operations using them. Code Description: The code below is a greatly simplified