arraylist

How to remove duplicates from a list based on a custom java object not a primitive type?

心不动则不痛 提交于 2019-12-20 17:43:06
问题 Before I post this question, I found somehow similar question posted here. But the answer was based on a String. However, I have a different situation here. I am not trying to remove String but another object called AwardYearSource. This class has an int attribute called year. So I want to remove duplicates based on the year. i.e if there is year 2010 mentioned more than once, I want to remove that AwardYearSource object. How can I do that? 回答1: The simplest way to remove elements based on a

How to use ArrayList.addAll()?

 ̄綄美尐妖づ 提交于 2019-12-20 16:13:35
问题 I want to fill an ArrayList with these characters +,-,*,^ etc. How can I do this without having to add every character with arrayList.add() ? 回答1: Collections.addAll is what you want. Collections.addAll(myArrayList, '+', '-', '*', '^'); Another option is to pass the list into the constructor using Arrays.asList like this: List<Character> myArrayList = new ArrayList<Character>(Arrays.asList('+', '-', '*', '^')); If, however, you are good with the arrayList being fixed-length, you can go with

ListView is not showing correct values after scrolling

▼魔方 西西 提交于 2019-12-20 14:44:05
问题 In my application I am using a CustomListView with an ArrayAdapter to show different countries time. But after 6 to 7 rows(depending on the phone screen size) the time values are repeating. According to some previous post I have written the following code snippet to get the solution. But the problem is still there. Following is the code I have written: public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; Order o = items.get(position); if (v == null) {

Java convert ArrayList to string and back to ArrayList?

末鹿安然 提交于 2019-12-20 12:28:51
问题 I wanted to save an ArrayList to SharedPreferences so I need to turn it into a string and back, this is what I am doing: // Save to shared preferences SharedPreferences sharedPref = this.getPreferences(Activity.MODE_PRIVATE); SharedPreferences.Editor editor = this.getPreferences(Activity.MODE_PRIVATE).edit(); editor.putString("myAppsArr", myAppsArr.toString()); editor.commit(); I can retrieve it with String arrayString = sharedPref.getString("yourKey", null); but I don't know how to convert

Significant differences in Array vs Array List? [duplicate]

大兔子大兔子 提交于 2019-12-20 11:53:34
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: When to use ArrayList over array[] in c#? From the perspective of memory or processor costs, does there appear to be a significant difference between an array and an arrayList object? 回答1: An array is a low-level data structure that essentially maps to a region in memory. An ArrayList is a variable length list implemented as an array of object that is re-allocated as the list grows. ArrayList therefore has some

Compare every item to every other item in ArrayList

非 Y 不嫁゛ 提交于 2019-12-20 11:43:01
问题 I'm having trouble with what I thought should be a pretty simple problem. I need to compare every item in an arrayList with every other item in the the list without comparing items to themselves. It's not as simple as calling an equals() comparison, it involves some custom logic that I've omitted from my code below. Also the ArrayList should not be changed in any way. The problem I seem to be having is that once I get into the second loop, I don't know if I have another object to compare to

Reverse iteration through ArrayList gives IndexOutOfBoundsException

亡梦爱人 提交于 2019-12-20 11:05:12
问题 When I reverse iterate over an ArrayList I am getting a IndexOutOfBoundsException. I tried doing forward iteration and there is no problem. I expect and know that there are five elements in the list. The code is below: Collection rtns = absRtnMap.values(); List list = new ArrayList(rtns); Collections.sort(list); for(int j=list.size();j>0;j=j-1){ System.out.println(list.get(j)); } Forward iteration - which is working fine, but not useful for me: for(int j=0;j<list.size();j++){ System.out

Finding Largest String in ArrayList

三世轮回 提交于 2019-12-20 10:54:34
问题 // Im trying to find the largest String in my ArrayList and print it out and also to include what index the largest element resides at and to print that to screen too. Im just wondering where Im going wrong. Thanks. import java.util.Scanner; import java.util.ArrayList; public class ArraylistString { public static void main(String [] args) { // Instance of Scanner class Scanner keyboardIn = new Scanner(System.in); // Declare an array list of Strings ArrayList<String> Str = new ArrayList<>(); /

Creating instance list of different objects

北城余情 提交于 2019-12-20 10:38:39
问题 I'm tring to create an arraylist of different class instances. How can I create a list without defining a class type? (<Employee>) List<Employee> employees = new ArrayList<Employee>(); employees.add(new Employee()); Employee employee = employees.get(0); 回答1: You could create a list of Object like List<Object> list = new ArrayList<Object>() . As all classes implementation extends implicit or explicit from java.lang.Object class, this list can hold any object, including instances of Employee ,

How can I group an array of rectangles into “Islands” of connected regions?

心不动则不痛 提交于 2019-12-20 10:36:36
问题 The problem I have an array of java.awt.Rectangle s. For those who are not familiar with this class, the important piece of information is that they provide an .intersects(Rectangle b) function. I would like to write a function that takes this array of Rectangle s, and breaks it up into groups of connected rectangles. Lets say for example, that these are my rectangles (constructor takes the arguments x , y , width , height ): Rectangle[] rects = new Rectangle[] { new Rectangle(0, 0, 4, 2), /