How-to articles, tricks, and solutions about ARRAYS

How to convert jsonString to JSONObject in Java

To convert a JSON string to a JSONObject in Java, you can use the JSONObject constructor that takes a String as an argument, like this:

How to convert object array to string array in Java

To convert an object array to a string array in Java, you can use the toString() method of the Object class and the map() method of the Stream class.

How to create a generic array in 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 a sub array from another array in Java?

There are a few different ways to create a subarray from another array in Java:

How to create ArrayList from array in Java

To create an ArrayList from an array in Java, you can use the ArrayList constructor that takes an Collection as an argument.

How to create correct JSONArray in Java using JSONObject

To create a JSON array using JSONObject in Java, you can use the put() method of the JSONObject class to add elements to the array.

How to declare and add items to an array in Python?

To declare an array in Python, you can use the array module.

How to find array / dictionary value using key?

In PHP, you can use the $array[$key] syntax to get the value stored at a particular key in an array.

How to find average from array in php?

You can use the array_sum function to get the sum of all the values in an array, and then divide that sum by the number of elements in the array to get the average.

How to find the index of an element in an int array?

To find the index of an element in an int array in Java, you can use the indexOf() method of the Arrays class.

How to fix Warning Illegal string offset in PHP

The "Illegal string offset" warning in PHP occurs when you try to access an offset of a string variable as if it were an array.

How to Flatten a Multidimensional Array?

You can use array_walk_recursive() function in PHP to flatten a multidimensional array.

How to get last key in an array?

You can use the end() function to get the last element of an array in PHP.

How to initialize an array in Java?

There are several ways to initialize an array in Java. Here are some examples: Using the new operator:

How to initialize an array of objects in Java

Here is an example of how you can initialize an array of objects in Java:

How to pass an array via $_GET in php?

In PHP, arrays can be passed in the URL using the $_GET superglobal variable, but they need to be encoded first using the http_build_query() function.

How to put a Scanner input into an array... for example a couple of numbers

To put input from a Scanner into an array in Java, you can use a loop to read the input and store it in the array.

How to read a text file into a list or an array with Python

One way to read a text file into a list or an array with Python is to use the split() method.

How to remove a variable from a PHP session array

To remove a variable from a PHP session array, you can use the unset() function.

How to remove duplicate values from a multi-dimensional array in PHP

To remove duplicate values from a multi-dimensional array in PHP, you can use the following approach:

How to split a String by space

In Java, you can split a string by space using the split() method of the String class. This method takes a regular expression as an argument and returns an array of substrings split by the regular expression.

How to use php array with sql IN operator?

You can use the implode() function to convert the PHP array into a string of comma-separated values, and then use that string in the SQL query with the IN operator.

In Twig, check if a specific key of an array exists

In Twig, you can use the exists function to check if a specific key of an array exists.

Is arr.__len__() the preferred way to get the length of an array in Python?

No, in python the built-in len() function is the preferred way to get the length of an array, list or any other iterable object.

Java Array Sort descending?

To sort an array in descending order in Java, you can use the Arrays.sort method and pass it a comparator that compares the elements in reverse order.