Snippets tagged “arrays”
76 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
- Any shortcut to initialize all array elements to zero?Java
To initialize all elements of an array to zero in Java, you can use the Arrays.fill method from the java.util package.
- ArrayList of int array in javaJava
To create an ArrayList of int arrays in Java, you can use the following syntax:
- Convert an integer to an array of digitsJava
To convert an integer to an array of digits in Java, you can use the toCharArray() method of the String class to convert the integer to a string, and then use the toCharArray() method to convert the string to an array of characters.
- Convert String array to ArrayListJava
To convert a String array to an ArrayList in Java, you can use the following steps:
- Fatal error: [] operator not supported for stringsPHP
This error is usually caused when you are trying to use the square brackets [] to access a character in a string, but this is not supported in PHP.
- Finding the max/min value in an array of primitives using JavaJava
To find the maximum value in an array of primitives in Java, you can use the Arrays.stream() method to create a stream from the array, and then use the Stream.max() method to find the maximum element in the stream. Here is an example of how to do this for
- How can I check whether an array is null / empty?Java
To check if an array is null in Java, you can use the == operator and compare the array to null. For example:
- How can I concatenate two arrays in Java?Java
To concatenate two arrays in Java, you can use the System.arraycopy method. Here's an example of how you can do this:
- How can I convert List<Integer> to int[] in Java?Java
You can use the toArray() method of the List interface to convert a List<Integer> to an int[] in Java.
- How can I force PHP to use strings for array keys?PHP
In PHP, array keys are typically automatically cast to integers if they are numeric, and to strings if they are not.
- How can the Euclidean distance be calculated with NumPy?Python
Here is a code snippet that shows how to calculate the Euclidean distance using NumPy:
- How do I determine whether an array contains a particular value in Java?Java
To check if an array contains a particular value in Java, you can use the contains() method of the List interface, which is implemented by the ArrayList class.
- How do I join two lists in Java?Java
There are several ways to join two lists in Java. Here are a few options:
- How do I reverse an int array in Java?Java
To reverse an int array in Java, you can use a loop to swap the elements at the beginning and end of the array, and then move the pointers inward until they meet in the middle of the array.
- How does the Java 'for each' loop work?Java
The Java for-each loop, also known as the enhanced for loop, is a convenient way to iterate over the elements of an array or a collection. It eliminates the need to use an index variable to access the elements of the collection.
- How to Append an Item to an Array in JavaScriptJavaScript
On this page, you can learn the ways of appending an item or multiple items to an array in JavaScript.
- How to check if PHP array is associative or sequential?PHP
To check if an array is associative or sequential in PHP, you can use the array_keys() function and compare the resulting array of keys with the original array.
- How to Check Whether an Array Is Empty in PHPPHP
On this page, we represented to you the three principal ways that allow programmers to detect whether a given array is empty or not. Just see the examples.
- How to Compare Two JavaScrpt ArraysJavaScript
You can either loop through the arrays or convert them to string and then compare. This tutorial provides several fast methods of comparing two arrays.
- 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 a Comma-Separated String into ArrayJavaScript
Read this tutorial and learn information about the JavaScript built-in split() method which is called for converting a comma-separated string in an array.
- How to convert a Java 8 Stream to an Array?Java
To convert a Java 8 Stream to an array, you can use the toArray() method of the Stream interface.
- How to Convert a PHP Array to a JavaScript ArrayPHP
In this tutorial, you can find a comprehensive guideline on how to convert a PHP array to JavaScript accurately. Just check out the options and examples.
- How to convert an Array to a Set in JavaJava
To convert an array to a Set in Java, you can use the Arrays.asList() method to create a List from the array, and then use the List.toSet() method to create a Set from the List.
- How to convert an int array to String with toString method in JavaJava
To convert an int array to a string in Java, you can use the Arrays.toString() method from the java.util package.
- How to Convert an Integer into a String with PHPPHP
In this short guide, we will assist you to deal with one of the common issues in PHP: converting an integer into a string. Read it and follow the examples.
- 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 convert comma-separated String to List?Java
To convert a comma-separated String to a List in Java, you can use the String.split() method to split the string into an array of substrings, and then use the Arrays.asList() method to create a list from that array.
- How to convert int[] into List<Integer> in Java?Java
To convert an int[] array into a List<Integer> in Java, you can use the Arrays.stream() method to create a stream of the array, and then use the mapToObj() method to map each element of the stream to an Integer object.
- How to convert object array to string array in JavaJava
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 Count All the Array Elements with PHPPHP
While working with arrays, it is commonly necessary to count all its elements. This snippet will discover two helpful functions that will help you do that.
- How to create a sub array from another array in Java?Java
There are a few different ways to create a subarray from another array in Java:
- How to Create and Parse JSON Data with PHPPHP
JSON is a data-interchange format, which can be generated and parsed by PHP or other programming languages. See how to create and parse JSON data by PHP.
- 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 Deal With Undefined Offset Error in PHPPHP
Here is a snippet that will help you to deal with a common PHP undefined offset error. Here, you will find three methods that help to avoid such an error.
- 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 define a two-dimensional array?Python
A two-dimensional array in Python can be defined using the numpy library.
- How to Determine the First and Last Iteration in a Foreach loop?PHP
In this snippet, we are going to figure out how to successfully determine the first and last iteration in a foreach loop with the help of PHP.
- How to Empty an Array in JavaScriptJavaScript
This tutorial provides useful information about clearing an existing array in JavaScript. Get familiar to multiple methods and find the best one for you.
- How to Find the foreach Index with PHPPHP
Here is a short tutorial where you can find three helpful methods allowing to find the foreach index with the help of PHP.
- How to find the index of an element in an int array?Java
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 the "Invalid Argument Supplied for Foreach()" PHP ErrorPHP
The "invalid argument supplied for foreach()" is considered a common PHP error. With the help of this tutorial, you will figure out how to resolve it.
- How to Fix the Uncaught Error "Cannot Use Object of Type stdClass as an Array"PHP
This short tutorial is dedicated to a common PHP issue "cannot use object of Type stdClass as an array". Read on to find the most accurate solutions.
- How to Get the First Element of an Array in PHPPHP
This snippet gives you an opportunity to learn how to get the first element of an array using PHP. You can choose among the provided functions.
- How to Get the Last Item in an ArrayJavaScript
Read the tutorial and learn the methods of getting the last item in the array. Get to know the methods and their peculiarities to find the best solution.
- How to get the separate digits of an int number?Java
To get the separate digits of an integer number, you can convert the number to a string, and then use the list() function to convert the string to a list of characters.
- 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 an array in Java?Java
There are several ways to initialize an array in Java. Here are some examples: Using the new operator:
- How to initialize HashSet values by construction?Java
To initialize the values of a HashSet when constructing the set, you can use one of the HashSet constructors that takes a Collection as an argument.
- How to JSON Decode a String into an Array with PHPPHP
This is a short guideline that provides comprehensive information on how to decode a JSON string into an array with the help of PHP.
- How to Merge Two Arrays in JavaScript and De-duplicate ItemsJavaScript
Read this tutorial and find useful information about the simplest methods that are used for merging two arrays and removing duplicate items in JavaScript.
- How to Push Both Value and Key into a PHP ArrayPHP
In this short tutorial, you will find comprehensive solutions on how to push both value and key into a PHP array.
- How to Remove Objects from a JavaScript Associative ArrayJavaScript
JavaScript objects can be taught of associative arrays mapping keys to values. Read and find the method used to remove objects from an associative array.
- How to Sort an Array of Associative Arrays by Value of a Given Key in PHPPHP
If you want to learn how to sort an array of associative arrays by value of a given key in PHP, then read our tutorial. Here, you can find handy solutions.
- How to Sort an Array of IntegersJavaScript
Read this JavaScript tutorial and learn useful information about the logic of the sort() method which will help you sort an array of integers correctly.
- How to split a string with any whitespace chars as delimitersJava
In Java, you can use the split() method of the String class to split a string based on a regular expression.
- How to use php array with sql IN operator?PHP
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.
- How to Use serialize() and unserialize() in PHPPHP
This tutorial can be quite helpful for you, if you are interested in using the PHP serialize() and unserialized() functions in your programming practice.
- How to Use the expm1() Function in PHPPHP
In this snippet, you will find comprehensive and helpful guidelines on how to use the expm1() function in PHP.
- Is JavaScript a Pass-by-reference or Pass-by-value language?JavaScript
On this page, we will look into pass-by-reference and pass-by-value. Let’s explore their differences and find out what language JavaScript is considered.
- Java Compare Two ListsJava
To compare two lists in Java, you can use the equals() method of the List interface.
- Java: how to initialize String[]?Java
There are several ways to initialize a String array in Java. Here are a few examples:
- JSON_ENCODE of multidimensional array giving different resultsPHP
JSON_ENCODE is a function that converts a PHP data structure, such as an array, into a JSON (JavaScript Object Notation) string.
- PHP - Check if two arrays are equalPHP
You can use the array_diff() function to check if two arrays are equal.
- PHP: Check if an array contains all array values from another arrayPHP
You can use the array_diff function in PHP to compare two arrays and check if the first array contains all of the values from the second array.
- PHPUnit: assert two arrays are equal, but order of elements not importantPHP
You can use the assertEqualsCanonicalizing or assertEqualsIgnoringCase methods to compare two arrays and ignore the order of the elements.
- Random shuffling of an arrayJava
To shuffle an array randomly in Java, you can use the Collections.shuffle method.
- Remove Item from ArrayListJava
To remove an item from an ArrayList in Java, you can use the remove() method of the ArrayList class.
- Returning Arrays in JavaJava
To return an array in Java, you can simply specify the array type as the return type of the method.
- Run process with realtime output in PHPPHP
In PHP, you can use the proc_open function to run a process and read its output in real-time.
- Sort an array in JavaJava
To sort an array in Java, you can use the Arrays.sort() method of the java.util.Arrays class. This method sorts the elements of the array in ascending order according to their natural ordering.
- Twig for loop for arrays with keysPHP
To loop through an array with keys in Twig, you can use the for loop and access the key and value of each element in the array using the loop variable.
- ValueError: setting an array element with a sequencePython
This code creates a 2D numpy array, and then tries to set the first element of the array to a list.
- What's the simplest way to print a Java array?Java
There are several ways to print an array in Java. Here are a few options: Using a loop