Get specific ArrayList item
public static ArrayList mainList = someList; How can I get a specific item from this ArrayList? mainList[3]? 8 Answers 8
public static ArrayList mainList = someList; How can I get a specific item from this ArrayList? mainList[3]? 8 Answers 8
I had this code: String[] lineElements; . . . try { using (StreamReader sr = new StreamReader(“TestFile.txt”)) { String line; while ((line = sr.ReadLine()) != null) { lineElements = line.Split(‘,’); . . . but then thought I should maybe go with a List instead. But this code: List<String> listStrLineElements; . . . try { using … Read more
How can I check if a value that is written in scanner exists in an ArrayList? List<CurrentAccount> lista = new ArrayList<CurrentAccount>(); CurrentAccount conta1 = new CurrentAccount(“Alberto Carlos”, 1052); CurrentAccount conta2 = new CurrentAccount(“Pedro Fonseca”, 30); CurrentAccount conta3 = new CurrentAccount(“Ricardo Vitor”, 1534); CurrentAccount conta4 = new CurrentAccount(“João Lopes”, 3135); lista.add(conta1); lista.add(conta2); lista.add(conta3); lista.add(conta4); Collections.sort(lista); System.out.printf(“Bank … Read more
I try with a loop like that // ArrayList tourists for (Tourist t : tourists) { if (t != null) { t.setId(idForm); } } But it isn’t nice. Can anyone suggest me a better solution? Some useful benchmarks to make better decision: While loop, For loop and Iterator Performance Test 18 Answers 18
I am wanting to create an array of arraylist like below: ArrayList<Individual>[] group = new ArrayList<Individual>()[4]; But it’s not compiling. How can I do this? 20 Answers 20
So, normally ArrayList.toArray() would return a type of Object[]….but supposed it’s an Arraylist of object Custom, how do I make toArray() to return a type of Custom[] rather than Object[]? 6 Answers 6
I have an ArrayList, a Collection class of Java, as follows: ArrayList<String> animals = new ArrayList<String>(); animals.add(“bat”); animals.add(“owl”); animals.add(“bat”); animals.add(“bat”); As you can see, the animals ArrayList consists of 3 bat elements and one owl element. I was wondering if there is any API in the Collection framework that returns the number of bat occurrences … Read more
I use a lot of lists and arrays but I have yet to come across a scenario in which the array list couldn’t be used just as easily as, if not easier than, the linked list. I was hoping someone could give me some examples of when the linked list is notably better. 15 Answers … Read more
This question already has answers here: Sort ArrayList of custom Objects by property (29 answers) Closed 8 years ago. I have a class named Fruit. I am creating a list of this class and adding each fruit in the list. I want to sort this list based on the order of fruit name. public class … Read more
This question already has answers here: Initialization of an ArrayList in one line (33 answers) Closed 6 years ago. ArrayList or List declaration in Java has questioned and answered how to declare an empty ArrayList but how do I declare an ArrayList with values? I’ve tried the following but it returns a syntax error: import … Read more