arraylist

problem with ArrayList in Java

a 夏天 提交于 2020-01-04 05:43:30
问题 I have issues getting my ArrayList to add properly. When I print the ArrayList after the for loop has finished, the ArrayList is the correct length, but every element is the same (the last Coordinate that was created). Can someone fix (and explain) the code below? public class test { private static ArrayList<Coordinate> mOrigCoords; private static ArrayList<Coordinate> mNewCoords; private static int mListSize; private static int mPixelsX; public static void main(String[] args) { mOrigCoords =

problem with ArrayList in Java

淺唱寂寞╮ 提交于 2020-01-04 05:43:25
问题 I have issues getting my ArrayList to add properly. When I print the ArrayList after the for loop has finished, the ArrayList is the correct length, but every element is the same (the last Coordinate that was created). Can someone fix (and explain) the code below? public class test { private static ArrayList<Coordinate> mOrigCoords; private static ArrayList<Coordinate> mNewCoords; private static int mListSize; private static int mPixelsX; public static void main(String[] args) { mOrigCoords =

Java 位平方和

对着背影说爱祢 提交于 2020-01-04 05:32:36
把一个整数的每个数位都平方后求和,又得到一个整数,则称这个整数为:位平方和。 题目: 平方怪圈 如果把一个正整数的每一位都平方后再求和,得到一个新的正整数。 对新产生的正整数再做同样的处理。 如此一来,你会发现,不管开始取的是什么数字, 最终如果不是落入1,就是落入同一个循环圈。 请写出这个循环圈中最大的那个数字。 请填写该最大数字。 注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。(第七届蓝桥杯) 1 import java.util.ArrayList; 2 import java.util.Scanner; 3 4 public class three { 5 static int max=0;//最大值 6 static ArrayList arrlist=new ArrayList();//存下每一次的平方和 7 static ArrayList quan=new ArrayList();//循环圈 8 9 public static void f(int n){ 10 int sum=0; 11 while(n!=0){ 12 sum+=(n%10)*(n%10); 13 n=n/10; 14 } 15 if(sum!=0) { 16 if (!arrlist.contains(sum)) {//如果没有出现平方和 17 arrlist.add(sum

lintcode:完美平方

让人想犯罪 __ 提交于 2020-01-04 05:28:53
题目 给一个正整数 n, 找到若干个完全平方数(比如1, 4, 9, ... )使得他们的和等于 n。你需要让平方数的个数最少。 样例 给出 n = 12 , 返回 3 因为 12 = 4 + 4 + 4 。 给出 n = 13 , 返回 2 因为 13 = 4 + 9 。 解题 题目标签:深度优先遍历 下面的深搜,通过找到所有结果,再找出最短的那个 public class Solution { /** * @param n a positive integer * @return an integer */ public int numSquares(int n) { // Write your code here int up = (int)Math.sqrt(n); ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> list = new ArrayList<Integer>(); int sum = 0; dfs(result,list,up,sum,n); int min = result.get(0).size(); for(int i=0;i<result.size();i++){ min = Math.min(min

adapters notifyDataSetChanged does not work

穿精又带淫゛_ 提交于 2020-01-04 02:53:28
问题 EDIT 2: I did solve my problem, but i don't know how:S I was moving my code snippets a bit around, a suddenly it worked. Must have done something in the wrong order, but its weird, checked it many times. Thanks for you help, and sorry I can't post an answer ;) Hi. I have a list view which I'm trying to refresh to update it self when i add an element to the underlying array list. Here is the code snippet: private void addEvent() { arrlEvents.add( event ); adptEvents.notifyDataSetChanged();

Passing ArrayList to PHP using Android Asynchronous Http Client Issue

依然范特西╮ 提交于 2020-01-04 02:26:06
问题 Im trying to send ArrayList to PHP ($_POST) and use it as an array. I use Android Asynchronous Http Client http://loopj.com/android-async-http/ My code ArrayList<String> array = new ArrayList<String>(); array.add("1"); array.add("2"); array.add("3"); RequestParams params = new RequestParams(); params.put("descr_array", array ); AsyncHttpClient client = new AsyncHttpClient(); client.post(StaticValues.SERVER_PAGE, params, new AsyncHttpResponseHandler() { ... } In PHP $descrarray[] = $_POST[

indexOf or Binary Search? [duplicate]

我们两清 提交于 2020-01-04 02:19:25
问题 This question already has answers here : Collections.binarySearch() vs. List indexOf() (3 answers) Closed 4 years ago . I have an array list of strings, the string are in sorted order, i want to find the index of a particular element, Which one will be faster? Performing indexOf() of lists Performing a binary search in an equivalent array? 回答1: You can use Collections.binarySearch directly for better efficience: public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T

Blackberry JDE ArrayList?

折月煮酒 提交于 2020-01-04 01:20:32
问题 The Blackberry JDE does not include java.util.ArrayList , even though it knows about java.util ? What's up with that? Is there an equivalent class for BB? I don't want to use an array, really, because I have an unknown number of objects I'm dealing with. why does the Blackberry JDE leave so much out? 回答1: Well they are Java from a language standpoint. It just doesn't support all of the standard edition packages. It falls more inline with the microedition standards, but is way beyond J2ME from

【源码解析】扒开ArrayList的外衣

烈酒焚心 提交于 2020-01-03 20:00:46
积千里跬步,汇万里江河;每天进步一点点,终有一天将成大佬。 本文内容 当然ArrayList里的方法不止这些,本文主要讲一些常用的方法 方法变量 Arraylist 里的方法变量主要有以下几个 1. 构造方法 1.1 有参构造 1.1.1 传入数组的大小 1.1.1.1代码实现 List < String > list = new ArrayList < > ( 5 ) ; 1.1.1.2源码解析 1.1.2 传入一个list对象 其实这个就相当于把传入的list对象里的数据 复制 到新的ArrayList对象 1.1.2.1 代码实现 List < String > list = new ArrayList < > ( Arrays . asList ( "z" , "m" , "h" ) ) ; 这里用来 Arrays 工具类里的 asList 方法,它的源码里是直接返回一个List,有兴趣的可以去看看,这里就不介绍了 1.1.2.2 源码解析 1.2 无参构造 这个比较简单,直接赋值一个空数组 1.2.1代码实现 List < String > list = new ArrayList < > ( ) ; 1.2.2源码解析 2. add方法 add一般常用的有两个方法,一个就是 add(E e) 在尾部添加数据,一个就是 add(int index,E element)

Sorting ArrayList of Objects using Hashcode

醉酒当歌 提交于 2020-01-03 19:17:02
问题 I have an ArrayList of Objects(POJOs) that have an Id and another field. I have implemented the equals()/hashcode() override in the POJO for the Id field. When I compare two objects using the equals() method of the Object class, it works perfectly fine. However when I add these objects to an arraylist and implement the Collections.sort(arrListOfObjects); it gives me a classCastexception. I looked up and found that I need to implement a Comparator. This comparator also does something to equals