arraylist

binary search tree path list

僤鯓⒐⒋嵵緔 提交于 2021-01-27 13:47:36
问题 This is code to get all the root to leaf paths in a Binary Tree but it puts in all the paths concatenated into one path. What is going wrong with the recursive call? private void rec(TreeNode root,List<Integer> l, List<List<Integer>> lists) { if (root == null) return; if (root.left == null && root.right == null ) { l.add(root.val); lists.add(l); } if (root.left != null) { l.add(root.val); rec(root.left,l,lists); } if (root.right != null) { l.add(root.val); rec(root.right,l,lists); } } 回答1:

Why ArrayList's non-static inner class SubList has a member variable “parent”?

折月煮酒 提交于 2021-01-27 07:20:50
问题 java.util.ArrayList.SubList is a non-static inner class class of java.util.ArrayList, which means that it holds an reference to its enclosing class. We can access the members of java.util.ArrayList by using ArrayList.this. But java.util.ArrayList.SubList also has a member "parent" which is also a reference to the enclosing class of java.util.ArrayList.SubList. Why the "parent" member variable is needed or why not declare java.util.ArrayList.SubList as a static inner class? My jdk is the

How to convert List<NameValuePair> into a hashMap<String, String>?

本小妞迷上赌 提交于 2021-01-27 07:07:26
问题 I'm capturing parameters from a request url using com.apache.http.NameValuePair which basically store those params in List<NameValuePair> . To do certain checks and verifications on those params, I need to convert that list into a HashMap<String, String> . Is there a way to do this conversion? 回答1: Do you use Java 8? In that case you could make use of the Collectors.toMap() method: Map<String, String> mapped = list.stream().collect( Collectors.toMap(NameValuePair::getName, NameValuePair:

Java - A method that takes vararg and returns arraylist?

本秂侑毒 提交于 2021-01-27 04:18:08
问题 I'm not entirely comfortable with generics and thus haven't found a solution to this yet. I have these three methods: public static List<ObjectA> objectAAsList(ObjectA ... items) { return new ArrayList<>(Arrays.asList(items)); } public static List<ObjectB> objectBAsList(ObjectB ... items) { return new ArrayList<>(Arrays.asList(items)); } public static List<ObjectC> objectCAsList(ObjectC ... items) { return new ArrayList<>(Arrays.asList(items)); } How can I create a single method that takes a

Java - A method that takes vararg and returns arraylist?

拜拜、爱过 提交于 2021-01-27 04:17:17
问题 I'm not entirely comfortable with generics and thus haven't found a solution to this yet. I have these three methods: public static List<ObjectA> objectAAsList(ObjectA ... items) { return new ArrayList<>(Arrays.asList(items)); } public static List<ObjectB> objectBAsList(ObjectB ... items) { return new ArrayList<>(Arrays.asList(items)); } public static List<ObjectC> objectCAsList(ObjectC ... items) { return new ArrayList<>(Arrays.asList(items)); } How can I create a single method that takes a

Java - A method that takes vararg and returns arraylist?

半世苍凉 提交于 2021-01-27 04:16:50
问题 I'm not entirely comfortable with generics and thus haven't found a solution to this yet. I have these three methods: public static List<ObjectA> objectAAsList(ObjectA ... items) { return new ArrayList<>(Arrays.asList(items)); } public static List<ObjectB> objectBAsList(ObjectB ... items) { return new ArrayList<>(Arrays.asList(items)); } public static List<ObjectC> objectCAsList(ObjectC ... items) { return new ArrayList<>(Arrays.asList(items)); } How can I create a single method that takes a

ParseUser.getList() in parse database

余生颓废 提交于 2021-01-07 01:41:35
问题 I am creating a twitter like app where we can follow other users and read their tweets. ParseUser.getCurrentUser().getList("isFollowing").remove(users.get(position)); List<String> tmpUsers=ParseUser.getCurrentUser().getList("isFollowing"); ParseUser.getCurrentUser().remove("isFollowing"); ParseUser.getCurrentUser().put("isFollowing", tmpUsers); The above code runs when user wants to unfollow. I had a doubt in: ParseUser.getCurrentUser().getList("isFollowing").remove(users.get(position)); List

How in groovy (java) automatically find out the json field type and return values from json, replacing null with empty space?

筅森魡賤 提交于 2021-01-05 09:13:43
问题 The question is still relevant! In my task, json comes to my input, which I do not know in advance. I need to collect all json field types into "types" and return all values ​​using reader.outputLines . Now the list of json field types is formed like this: def types = list.find (). Values ​​() *. GetClass () *. SimpleName But I am faced with a problem when the same field in the first json block is null, and in the second the integer and the type is defined as null, and not as Integer. How to

How in groovy (java) automatically find out the json field type and return values from json, replacing null with empty space?

£可爱£侵袭症+ 提交于 2021-01-05 09:12:00
问题 The question is still relevant! In my task, json comes to my input, which I do not know in advance. I need to collect all json field types into "types" and return all values ​​using reader.outputLines . Now the list of json field types is formed like this: def types = list.find (). Values ​​() *. GetClass () *. SimpleName But I am faced with a problem when the same field in the first json block is null, and in the second the integer and the type is defined as null, and not as Integer. How to

Is there any ways to save external file “information” into array in java?

无人久伴 提交于 2021-01-05 07:27:40
问题 e.g: (I took the first line of the external file) Giant, Colgate Toothpaste, 4.50 I want to separate & save them in an array like this before/after I send them to the object & ArrayList. mall[i] = "Giant"; product[i] = "Colgate Toothpaste"; price[i] = 4.50 p/s: I think I should do that because I need to alter the price in the future. This is how my coding looks like right now. public static void readFile(ArrayList<Product> productList) throws Exception { try { productList.clear(); //clear the