arraylist

Remove elements from arraylist based on a condition [closed]

江枫思渺然 提交于 2020-01-17 23:29:52
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . I have an ArrayList whose each element is of type DataType, where DataType is a class: class DataType{ String dId; String dType; String rId; } I need to remove all such elements from the list whose rId is equal to any other element's dID. i.e. if DataType D1 has value of dID as "abc" and DataType D2 has value of

使用ArrayList集合,对其添加100个不同的元素:

十年热恋 提交于 2020-01-17 20:27:29
import java.util.ArrayList; import java.util.Iterator; public class Demo { public static void main(String[] args) { ArrayList list=new ArrayList<>(); for(int i=1;i<=100;i+=1){ list.add(i); } Iterator it=list.iterator(); while(it.hasNext()){ System.out.print(" "+it.next()); } System.out.println(); try{ System.out.println(list.get(50)); System.out.println(list.get(90)); } catch (Exception e) { // TODO: handle exception } } } 作业: 使用ArrayList集合,对其添加100个不同的元素: 1.使用add()方法将元素添加到ArrayList集合对象中; 2.调用集合的iterator()方法获得Iterator对象,并调用Iterator的hasNext()和next()方法,迭代的读取集合中的每个元素; 3.调用get()方法先后读取索引位置为50和102的元素

How to populate a JComboBox from an ArrayList?

放肆的年华 提交于 2020-01-17 14:48:12
问题 I've got an ArrayList of objects, and all the objects have names. How can I populate a JComboBox with those names? I've looked online and found nothing so far. There are some resources, but they tend to go with a hardcoded version, which is useless. Since I don't have any code yet showing what I'm doing, I can't attach any. 回答1: JComboBox works with Array or Vector . You should use .toArray() method in the ArrayList to create an array. String[] names = namesList.toArray(new String[0]); then

Filter unique objects from an ArrayList based on property value of the contained object

℡╲_俬逩灬. 提交于 2020-01-17 02:39:46
问题 How will I filter unique object from an arraylist. List<LabelValue> uniqueCityListBasedState = new ArrayList<LabelValue>(); for (LabelValue city : cityListBasedState) { if (!uniqueCityListBasedState.contains(city)) { uniqueCityListBasedState.add(city); } } This is my code. But the problem is that I need to filter not on the object but on the value of a property inside that object. In this case, I need to exclude the objects that has the name. That is city.getName() 回答1: List<LabelValue>

How to prevent storing of text coming after special characters — in arraylist in java

眉间皱痕 提交于 2020-01-17 01:22:12
问题 I am reading a pl/sql code from a text file and storing all words of it into array list from below code : Scanner in1 = new Scanner(file1); ArrayList<String> Code1 = new ArrayList<String>(); in1.useDelimiter("/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/|[\\p{javaWhitespace}\\.|,]+"); while (in1.hasNext()) { Code1.add(in1.next().toLowerCase()); } Everything is working fine but i am facing issue whenever there is a comments section in code written in after special character -- Like below: select * from

Sending and receiving ArrayList Objects or Array Objects in Netty

别等时光非礼了梦想. 提交于 2020-01-16 20:09:07
问题 Hello all I am really new to netty. What I want is something like this. On my client I want to enter a String query example SELECT * FROM drivers . I am using an mysql xampp server. Then my server will query it down and add it to arraylist private List<Drivers> getRecords(ResultSet rs) throws SQLException { List<Drivers> records = new ArrayList<Drivers>(); while(rs.next()){ records.add(new Drivers(rs.getInt("first_name"), rs.getString("last_name"))); } return records; } After that it will

Separatley representing arraylist indexes as Strings

感情迁移 提交于 2020-01-16 18:41:49
问题 So far I have an ArrayList that has the following contents [[2/11, 11/48], [8/35, 35/288], [16/43, 43/288], [75/152, 19/210], [4/5, 5/16], [135/163, 163/1680]] However I need to represent each individual part of this as a String representation such as the following, {true@2/11, false@9/11}@11/48 Where the false @ is the complement of true@, not so sure how to go about getting them seperatly out of the list, for example I tried knowledegeD.get(0) for the first bit but obviously that just

How do you pull specific objects from an arraylist?

∥☆過路亽.° 提交于 2020-01-16 18:35:10
问题 I've made an Animal superclass, Shark and Whale subclasses. What would I use to print out just the Shark objects from this arraylist? Driver: import java.util.ArrayList; public class Creator { public static void main(String[] args){ ArrayList<Animal> obj = new ArrayList<Animal>(); obj.add(new Shark("James")); obj.add(new Shark("Mike")); obj.add(new Whale("Steve")); obj.add(new Whale("Tommy")); for (Animal a: obj){ System.out.println(a.getName()); } } } 回答1: You can use the instanceof to check

arraylist sorting in order

删除回忆录丶 提交于 2020-01-16 18:18:23
问题 Attempting to sort arraylist in alphabetical order but the Collection.sort is giving me an error which i do not what to do with it import java.util.ArrayList; import java.util.Collection; /** * * @author user */ public class BookShelf { ArrayList<Book> listOfBooks = new ArrayList<Book>(); public void addBook(Book book) { listOfBooks.add(book); } public ArrayList<Book> returnListOfBooks() { ArrayList<Book> myBook = new ArrayList<Book>(listOfBooks); Collection.sort(myBook); return myBook; } any