arraylist

type erasure in implementation of ArrayList in Java

江枫思渺然 提交于 2020-01-01 04:23:12
问题 I was reading this article on Java Generics and there it is mentioned that the constructor for an ArrayList looks somewhat like this: class ArrayList<V> { private V[] backingArray; public ArrayList() { backingArray = (V[]) new Object[DEFAULT_SIZE]; } } I was not able to understand how type erasure and type checking by the compiler happens as explained there. One point I got was that the type parameter is transformed to Object type. I would imagine it as (replacing all V with Object ), but

Calculate average or take in an ArrayList as a parameter to a function

 ̄綄美尐妖づ 提交于 2020-01-01 03:43:23
问题 Is there a built-in method to calculate the average of an integer ArrayList? If not, can I make a function that will do that by taking in the name of the ArrayList and returning its average value? 回答1: It's really simple: // Better use a `List`. It is more generic and it also receives an `ArrayList`. public static double average(List<Integer> list) { // 'average' is undefined if there are no elements in the list. if (list == null || list.isEmpty()) return 0.0; // Calculate the summation of

Java question about ArrayList<Integer>[] x

最后都变了- 提交于 2020-01-01 03:34:06
问题 I have always had this one issue with arrays of ArrayLists. Maybe you can help. //declare in class private ArrayList<Integer>[] x; //in constructor x=new ArrayList[n]; This generates a warning about unchecked conversion. But x=new ArrayList<Integer>[n]; is a compiler error. Any idea? Thanks! 回答1: You can't make a array of generics lists. Fortunately, there are workarounds. And even more fortunately, there is a nice site about Generics with more information than you'd ever want to know. The

How do arrays “remember” their types in Java?

久未见 提交于 2020-01-01 01:59:13
问题 Consider the following code: class AA { } class BB extends AA { } public class Testing { public static void main(String[] args) { BB[] arr = new BB[10]; AA[] arr2 = arr; BB b = new BB(); AA a = new AA(); arr2[0] = a; // ArrayStoreException at runtime arr2[1] = b; List<BB> listBB = new ArrayList<>(); List listAA = listBB; listAA.add("hello world.txt"); } } In the above example, I get the ArrayStoreException when I try arr2[0] = a . That means the array remembers what type it must accept. But

Sorting custom class array-list string using Collections.sort

孤者浪人 提交于 2020-01-01 00:46:51
问题 I am trying to sort my custom class array-list using Collections.sort by declaring my own anonymous comparator. But the sort is not working as expected. My code is Collections.sort(arrlstContacts, new Comparator<Contacts>() { public int compare(Contacts lhs, Contacts rhs) { int result = lhs.Name.compareTo(rhs.Name); if(result > 0) { return 1; } else if (result < 0) { return -1; } else { return 0; } } }); The result is not in sorted order. 回答1: Like Adam says, simply do: Collections.sort(

彻底解决 Canvas 引起的 java.lang.unsupported operation exception, android.view.GLES20Canvas.clipPath(GLES20Canvas异常

泄露秘密 提交于 2019-12-31 23:52:33
======= 7 Failure [INSTALL_FAILED_INVALID_APK] 执行 adb install - r test . apk。 时出现错误 Failure [ INSTALL_FAILED_INVALID_APK ] 可能是apk的签名出现问题,这个在之前遇到过,需要给apk签名 今天安装多个apk都是出现这个错误,遂关闭系统对签名的检测 方法:修改 /system/build.prop 文件。 将 ro . install . 3rd _cert = true 修改为 ro . install . 3rd _cert = false 重启设备就可以安装apk了 =========6   安装失败:INSTALL_FAILED_VERIFICATION_FAILURE? Install_failed_verifcation_failure?     你必须允许未签名的应用。安装被Android。允许设置非市场的应用程序安装。 =========5    关于魅族MX3无法弹出Toast的问题     设置->应用->你的应用,里面有一个显示通知的选项,打上勾,应该就可以了,你试一下 ========== 4.3 Segmentation fault 问题浅谈 今天调试程序时遇到了一个问题,当我用GDB调试程序时出现了Segmentation fault

二叉树中和为某一值的路径【Java】

*爱你&永不变心* 提交于 2019-12-31 12:49:16
题目描述: 输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前) 问题描述: 很明显是树的遍历而且还是先根遍历(先序遍历/前序遍历)。 理由:路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径 第一种解法儿 import java.util.ArrayList; public class Solution { private ArrayList<ArrayList<Integer>> arrayLists = new ArrayList<>(); public static void main(String[] args) { Solution solution = new Solution(); TreeNode root = new TreeNode(10); TreeNode left1 = new TreeNode(6); TreeNode right1 = new TreeNode(12); TreeNode left2 = new TreeNode(5); TreeNode right2 = new TreeNode(9); TreeNode left3 = new TreeNode(5);

java 分解arraylist中单个对象 的属性名与值

我是研究僧i 提交于 2019-12-31 10:40:04
引用: http://hi.baidu.com/%CD%F5%C7%E7%CB%EF/blog/item/0791fbc89aa0c7e252664f8d.html /** * 获取对象属性,返回一个字符串数组 * * @param o 对象 * @return String[] 字符串数组 */ private String[] getFiledName(Object o) { try { Field[] fields = o.getClass().getDeclaredFields(); String[] fieldNames = new String[fields.length]; for (int i=0; i < fields.length; i++) { fieldNames[i] = fields[i].getName(); } return fieldNames; } catch (SecurityException e) { e.printStackTrace(); System.out.println(e.toString()); } return null; } /** * 使用反射根据属性名称获取属性值 * * @param fieldName 属性名称 * @param o 操作对象 * @return Object 属性值 */ private

put random number in the array list java [closed]

时间秒杀一切 提交于 2019-12-31 07:56:19
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . I'm trying to put all random number which I generated to the arraylist and print all the random number into single array static int pick; public static

put random number in the array list java [closed]

无人久伴 提交于 2019-12-31 07:55:07
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . I'm trying to put all random number which I generated to the arraylist and print all the random number into single array static int pick; public static