arraylist

Populating ArrayList

狂风中的少年 提交于 2020-01-03 18:03:50
问题 I am trying to populate an array list, however, my array list constantly equals 0, and never initializes, despite my declaring it over main(). This is my code. static ArrayList<Integer> array = new ArrayList<Integer>(10); //The parenthesis value changes the size of the array. static Random randomize = new Random(); //This means do not pass 100 elements. public static void main (String [] args) { int tally = 0; int randInt = 0; randInt = randomize.nextInt(100); //Now set the random value.

Populating ArrayList

女生的网名这么多〃 提交于 2020-01-03 18:03:13
问题 I am trying to populate an array list, however, my array list constantly equals 0, and never initializes, despite my declaring it over main(). This is my code. static ArrayList<Integer> array = new ArrayList<Integer>(10); //The parenthesis value changes the size of the array. static Random randomize = new Random(); //This means do not pass 100 elements. public static void main (String [] args) { int tally = 0; int randInt = 0; randInt = randomize.nextInt(100); //Now set the random value.

Given an array of integers [x0 x1 x2], how do you calculate all possible permutations from [0 0 0] to [x0 x1 x2]?

拥有回忆 提交于 2020-01-03 17:28:20
问题 I am writing a program that takes in an ArrayList and I need to calculate all possible permutations starting with a list of zeroes, up to the value in the corresponding input list. Does anyone know how to iteratively calculate these values? For example, given [ 1 2 ] as input, it should find and store the following lists: [0 0], [1 0], [1 1], [1 2], [0 1], [0 2] Thanks! 回答1: Here's a standard recursive generator: import java.util.Arrays; //... static void generate(int... limits) { generate

java - copying array list

眉间皱痕 提交于 2020-01-03 15:33:24
问题 So the below method I'm aware causes both variables to point to the same object, but if I did want to find the simplest method to has ArrayList s2 exactly the same as s1, how would I do this? public static void main(String[] args) { ArrayList<String> s1 = new ArrayList<String>(); for (int i = 1; i <= 3; i++) { s1.add(""+i); } ArrayList<String> s2 = s1; } 回答1: You should create s2 with s1 as parameter. public static void main(String[] args) { ArrayList<String> s1 = new ArrayList<String>(); for

how to remove particular data from ArrayList<Integer> in android

扶醉桌前 提交于 2020-01-03 15:33:20
问题 I have one Array List and contains some values like 2,3,4,5,6. now how to check if the value is present and want to delete that particular Value. please help me to do this. tnx in advance. I tried , ArrayList<Integer> Positions=new ArrayList<Integer>(); Positions.remove(6); but it shows an error. 回答1: Positions.remove(6); delete the item from particular position. So first you have to compare the item in arraylist using for loop and get the position of that item and call Positions.remove(that

Java集合之LinkedList

我是研究僧i 提交于 2020-01-03 07:22:17
上一篇写的是ArrayList,这一篇写一下LinkedList. 开宗明义,因为Vector已经被废弃了,所以list家族只剩下ArrayList和LinkedList两兄弟了,这里直接对比一下二位: ArrayList基于动态数组的实现,它长于随机访问元素,但是在中间插入和移除元素时较慢 LinkedList基于链表实现,在List中间进行插入和删除的代价较低,提供了优化的顺序访问。LinkedList在随机访问方面相对比较慢,但是它的特性集较ArrayList更大。 那么,到底特性集大在哪里呢? 这就是这篇博客的重点,在JAVA中,LinkedList可以作为我们最经常使用的两种数据结构来使用:栈和队列。Java中实现队列一般都是用LinkedList实现。 这是因为LinkedList的实现是一个双向链表,所以无论是FIFO还是FILO都可以实现。 (1)初始化: 先看一下LinkedList的继承情况吧: public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable 然后看一下LinkedList的成员变量 transient int size = 0; transient Node<E>

Sum the content of 2 list in Groovy

China☆狼群 提交于 2020-01-03 07:16:08
问题 I have two lists in Groovy and need to sum the contents of both. For example: list1 = [1,1,1] list2 = [1,1,1] I expected this result: total = [2,2,2] I try to sum with + operator o .sum method, but I have a concatenation of the lists. [1, 1, 1, 1, 1, 1] It's Groovy enough groovy or I need to loop each element of the lists? 回答1: Groovy's List.transpose() works like zip in some other languages. Try this: list1 = [1,2,3] list2 = [4,5,6] assert [list1, list2].transpose()*.sum() == [5,7,9] 回答2: I

Custom ListView with image, text and add item button

廉价感情. 提交于 2020-01-03 06:38:09
问题 I am quite new to Android and can't achieve this, been searching all day. Layout I'm trying to create. I have created the custom xml layout, I have found ways to add items on create, but I need the list to be empty and than when the button is pressed to add from a list. This is the layout: <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TableRow> <ImageView android:id="@+id/info_img_view"

How To Use Contains on Custom Type ArrayList in java?

十年热恋 提交于 2020-01-03 05:14:23
问题 In here how can I search name by using contains? I want to match my input result of arrayList by using .contains. This Program I create a class name DataType for using custom type data type. arrayList.add(new DataType(name,Integer.parseInt(age))); System.out.println("Search Nme : "); String name=new Scanner(System.in).nextLine(); if (arrayList.contains(new DataType(name))) { System.out.println("Find"); } Or, System.out.println("Search Nme : "); String name=new Scanner(System.in).nextLine();

How to serialize ArrayList 2x and dont over write the already present ArrayList JAVA

ぃ、小莉子 提交于 2020-01-03 05:11:28
问题 I'm stuck with making an admin panel for my program to serialize more objects, what it does is , it saves object(String,String) into arraylist and the arraylist is saved into file through serialization. When trying to add another object into the arraylist, The program first deserialize the file stuff into the arraylist, because by not doing this how would i access the objects that are already present in the file.? but instead something like this happens while adding object 3 (console) Index 0