Snippets tagged “arraylist”
39 snippets use this tag.
- add an element to int [] array in javaJava
To add an element to an array in Java, you can use one of the following methods:
- add string to String arrayJava
To add a string to a string array in Java, you can use the Arrays.copyOf() method to create a new array that is one element larger than the original array, and then use a loop to copy the elements of the original array into the new array. Here's an exampl
- ArrayList of int array in javaJava
To create an ArrayList of int arrays in Java, you can use the following syntax:
- Best way to convert an ArrayList to a stringJava
To convert an ArrayList to a string in Java, you can use the join method of the String class, which allows you to join the elements of the ArrayList into a single string. Here's an example of how you might do this:
- Can not deserialize instance of java.util.ArrayList out of START_OBJECT tokenJava
This error is usually encountered when trying to parse a JSON string that does not start with a JSON array, but rather a JSON object.
- Convert ArrayList<String> to String[] arrayJava
To convert an ArrayList<String> to a String[] array in Java, you can use the toArray method of the ArrayList class and pass it an empty array of the appropriate type:
- Convert Set to List without creating new ListJava
To convert a Set to a List in Java without creating a new List object, you can use the List constructor that takes a Collection as an argument.
- Convert String array to ArrayListJava
To convert a String array to an ArrayList in Java, you can use the following steps:
- Converting 'ArrayList<String> to 'String[]' in JavaJava
You can use the toArray() method of the ArrayList class to convert an ArrayList of strings to a string array. Here's an example:
- Get specific ArrayList itemJava
To get a specific item from an ArrayList in Java, you can use the get() method of the ArrayList class.
- How do I convert a Map to List in Java?Java
You can use the entrySet() method of the Map interface to get a Set view of the mappings contained in the map, and then create a List from this set using the new
- How do I remove repeated elements from ArrayList?Java
To remove repeated elements from an ArrayList in Java, you can use the removeAll method and pass it a Collection containing the elements to be removed.
- How to add new elements to an array in Java?Java
To add new elements to an array in Java, you have a few options: If you know the size of the array in advance and the array is not full, you can simply assign a new value to an unused element in the array.
- How to clone ArrayList and also clone its contents?Java
To clone an ArrayList and also clone its contents, you can use the clone method of the ArrayList class, which creates a shallow copy of the list.
- How to convert a Collection to List?Java
In Java, you can convert a Collection (such as Set, Queue, etc.) to a List using the following methods:
- How to convert an ArrayList containing Integers to primitive int array?Java
To convert an ArrayList containing Integer objects to a primitive int array in Java, you can use the toArray method and a casting operation.
- How to convert array to list in JavaJava
To convert an array to a list in Java, you can use the Arrays.asList() method. This method returns a fixed-size list backed by the specified array. Here's an example:
- How to create a generic array in Java?Java
In Java, it is not possible to create a generic array directly. However, you can create an array of a specific type and then cast it to a generic type.
- How to create ArrayList from array in JavaJava
To create an ArrayList from an array in Java, you can use the ArrayList constructor that takes an Collection as an argument.
- How to declare an ArrayList with values?Java
To declare an ArrayList with values in Java, you can use the Arrays.asList method and pass it an array or a list of values.
- How to find the length of an array list?Java
To find the length (number of elements) of an ArrayList in Java, you can use the size() method of the ArrayList class.
- How to get maximum value from the Collection (for example ArrayList)?Java
In Java, default methods are methods that are defined in an interface and have a default implementation. They were introduced in Java 8 as a way to add new functionality to interfaces without breaking backward compatibility.
- How to get the last value of an ArrayListJava
To get the last value of an ArrayList in Java, you can use the size() method to get the size of the list and then access the value at the index size() - 1.
- How to implement a tree data-structure in Java?Java
A tree is a data structure that consists of nodes arranged in a hierarchy. Each node has a value and may have zero or more child nodes. The top node in a tree is called the root node.
- How to Initialization of an ArrayList in one line in JavaJava
You can initialize an ArrayList in one line in Java using the following syntax:
- How to initialize List<String> object in Java?Java
To initialize a List<String> object in Java, you can use one of the following approaches: Using the new keyword:
- How to make a new List in JavaJava
In Java, you can create a new list using the java.util.ArrayList class or the java.util.LinkedList class.
- How to sort a List/ArrayList?Java
To sort a List or ArrayList in Java, you can use the sort method of the Collections class from the java.util package. The sort method takes a List and sorts it in ascending order according to the natural ordering of its elements.
- How to sort an ArrayList in JavaJava
To sort an ArrayList in Java, you can use the Collections.sort method.
- Initial size for the ArrayListJava
The initial size of an ArrayList in Java is 0.
- Java ArrayList copyJava
There are several ways to create a copy of an ArrayList in Java:
- Java dynamic array sizes?Java
In Java, you can implement a dynamic array (i.e., an array that can grow and shrink as needed) by using an ArrayList or an ArrayDeque.
- Java: how can I split an ArrayList in multiple small ArrayLists?Java
Here is an example of how you can split an ArrayList into multiple smaller ArrayLists in Java:
- Remove Item from ArrayListJava
To remove an item from an ArrayList in Java, you can use the remove() method of the ArrayList class.
- Removing an element from an Array in JavaJava
There are a few different ways you can remove an element from an array in Java. Here are a few options:
- Sort ArrayList of custom Objects by propertyJava
To sort an ArrayList of custom objects by a property, you can use the Collections.sort method and pass it a custom Comparator.
- Type List vs type ArrayList in JavaJava
In Java, List is an interface that defines a list data structure, while ArrayList is a class that implements the List interface.
- When to use LinkedList over ArrayList in Java?Java
In Java, both ArrayList and LinkedList are implementations of the List interface that allow you to store a collection of objects in a specific order. However, they are implemented differently and have different performance characteristics.
- Why do I get an UnsupportedOperationException when trying to remove an element from a List?Java
The java.lang.UnsupportedOperationException is thrown when an operation is not supported by a class.