arraylist

Java MySQL integration with ArrayLists

自古美人都是妖i 提交于 2019-12-22 13:01:51
问题 I am trying to get my program to be able to run over multiple runs. Meaning if I input something in my program and then shut it down, when I start it back up that info will still be there with the help of MySQL. I am fairly new to Java and MySQL but this is very important. My program works perfectly but I have no idea how to get MySQL to hold the arraylist. I already have the database set up with everything I need. This is my MySQL bridge so far: import java.sql.Connection; import java.sql

C# how to determine, whether ArrayList contains object with certain attribute

不打扰是莪最后的温柔 提交于 2019-12-22 12:37:08
问题 I have an ArrayList of objects of my custom class. I would like to know, if ArrayList contains object with certain attribute. I do not care about the object, just if there is some. Yes, I could do this with foreach cycle, but I was wondering if there was more elegant way to do so. Thanks for suggestions. 回答1: Well, to start with I'd suggest using List<T> instead of ArrayList . Then LINQ to Objects makes it really easy: if (list.Any(x => x.HasFoo)) { } Or without LINQ (but still List<T> ) if

Java ArrayList小记

浪尽此生 提交于 2019-12-22 11:34:45
1.基本用法   ArrayList是一个泛型容器,新建ArrayList需要实例化泛型参数,比如: ArrayList<String> StrList = new ArrayList<>(); ArrayList<Integer> intList = new ArrayList<>();   ArrayList的主要方法有: // 添加元素到末尾 public boolean add(E e) // 判断是否为空 public boolean isEmpty() // 获取大小 public int size() // 获取指定位置(索引)的元素 public E get(int index) // 查找指定元素,若找到,返回索引位置,否则返回-1 public int indexOf(Object o) // 从后往前找 public int lastIndexOf(Object o) // 判断是否包含指定元素,根据equals方法的返回值 public boolean contains(Object o) // 删除指定位置的元素,返回值为被删除的元素对象 public E remove(int index) // 删除指定元素,只删除第一个相同的元素,若o为null,则删除值为null的元素 public boolean remove(Object o) // 清空元素

Update Element in ArrayList/HashMap using java

你离开我真会死。 提交于 2019-12-22 11:24:37
问题 I'm doing some coursework for uni, I really should know this but I am unsure how I can update an object stored in a HashMap. I have an abstract 'User' class that extends into 'Customer' and 'Staff' classes, instances of which are stored in a HashMap named 'mapUsers'. The way I was thinking it could be done is saving the element to be modified into a temp 'User' object, on this temp instance I could modify the Object in any necessary way. My real question is, will this update the object stored

ArrayList和LinkedList的区别

懵懂的女人 提交于 2019-12-22 11:00:43
简单区别: 1.ArrayList是实现了基于动态数组的数据结构,LinkedList是基于双向链表的数据结构。 2.ArrayList实现了RandomAccess,数组支持随机访问, 查询速度快, 增删元素慢; LinkedList 没有实现 RandomAccess 接口,链表支持顺序访问, 查询速度慢, 增删元素快; 来源: CSDN 作者: walkmiss 链接: https://blog.csdn.net/huermiss/article/details/103650801

ArrayList Null Pointer Exception in onPostExecute()

六眼飞鱼酱① 提交于 2019-12-22 09:59:18
问题 Getting NullPointerException in ArrayList. I'm using the line below to log size of ArrayList but I always get NPE. What could be the reason? Log.d("catArrayList:Size:New", ""+categoryArrayList.size()); Here is some more code : protected void onPostExecute(Boolean result) { dialog.cancel(); Log.d("catArrayList:Size", ""+categoryArrayList.size()); Log.d("typArrayList:Size", ""+typeArrayList.size()); Log.d("serArrayList:Size", ""+serviceArrayList.size()); Log.d("cArrayList:Size", ""+cArrayList

How can I eliminate duplicate words from String in Java?

不想你离开。 提交于 2019-12-22 09:53:58
问题 I have an ArrayList of String s and it contains records such as: this is a first sentence hello my name is Chris what's up man what's up man today is tuesday I need to clear this list, so that the output does not contain repeated content. In the case above, the output should be: this is a first sentence hello my name is Chris what's up man today is tuesday as you can see, the 3rd String has been modified and now contains only one statement what's up man instead of two of them. In my list

Iterate over consecutive object pairs in an ArrayList

醉酒当歌 提交于 2019-12-22 09:52:33
问题 I want to get pairs of objects from an ArrayList so I can perform calculations between the elements of each object. Ideally it should iterate over pairs of objects. For example in a List with {obj1, obj2, obj3, obj4} it should go over {obj1,obj2}, {obj2,obj3} and {obj3,obj4}. What I have tried so far is as follows. public class Sum { public ArrayList<Double> calculateSum(ArrayList<Iter> iter) { ListIterator<Iter> it = iter.listIterator(); ArrayList<Double> sums = new ArrayList<>(); while (it

java - alphabetical order (list) [duplicate]

我只是一个虾纸丫 提交于 2019-12-22 08:44:19
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Sort List Alphabetically how do i store my inputs in alphabetical order, i am inputting names into an arraylist: persons.add(person); How to do that? 回答1: Try this: java.util.Collections.sort(people); 回答2: implements Comparator< T > interface class A implements Comparator < Person > { @Override public int compare(Person o1, Person o2) { if(o1.getName() != null && o2.getName() != null){ return o1.getName()

Java Arraylist remove multiple element by index

倾然丶 夕夏残阳落幕 提交于 2019-12-22 06:47:41
问题 Here is my code: for (int i = 0; i < myarraylist.size(); i++) { for (int j = 0; j < stopwords.size(); j++) { if (stopwords.get(j).equals(myarraylist.get(i))) { myarraylist.remove(i); id.remove(i); i--; // to look at the same index again! } } } I have problem.. after element removed, all index always changed, the loop above so messy. To illustrate: I have 54 data, but loop above become messy after element removed.. so only 50 data that checked. Is there another way or fix my code to remove