What’s the simplest way to print a Java array?

Since Java 5 you can use Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays. Note that the Object[] version calls .toString() on each object in the array. The output is even decorated in the exact way you’re asking. Examples: Simple Array: String[] array = new String[] {“John”, “Mary”, “Bob”}; System.out.println(Arrays.toString(array)); Output: [John, Mary, Bob] Nested Array: … Read more

Get all unique values in a JavaScript array (remove duplicates)

With JavaScript 1.6 / ECMAScript 5 you can use the native filter method of an Array in the following way to get an array with unique values: function onlyUnique(value, index, self) { return self.indexOf(value) === index; } // usage example: var a = [‘a’, 1, ‘a’, 2, ‘1’]; var unique = a.filter(onlyUnique); console.log(unique); // [‘a’, … Read more

How do I determine whether an array contains a particular value in Java?

Arrays.asList(yourArray).contains(yourValue) Warning: this doesn’t work for arrays of primitives (see the comments). Since java-8 you can now use Streams. String[] values = {“AB”,”BC”,”CD”,”AE”}; boolean contains = Arrays.stream(values).anyMatch(“s”::equals); To check whether an array of int, double or long contains a value use IntStream, DoubleStream or LongStream respectively. Example int[] a = {1,2,3,4}; boolean contains = IntStream.of(a).anyMatch(x … Read more

Deleting an element from an array in PHP

There are different ways to delete an array element, where some are more useful for some specific tasks than others. Deleting a single array element If you want to delete just one array element you can use unset() or alternatively \array_splice(). If you know the value and don’t know the key to delete the element … Read more

How can I check if an object is an array? [duplicate]

The method given in the ECMAScript standard to find the class of Object is to use the toString method from Object.prototype. if(Object.prototype.toString.call(someVar) === ‘[object Array]’) { alert(‘Array!’); } Or you could use typeof to test if it is a string: if(typeof someVar === ‘string’) { someVar = [someVar]; } Or if you’re not concerned about … Read more

Loop through an array in JavaScript

Three main options: for (var i = 0; i < xs.length; i++) { console.log(xs[i]); } xs.forEach((x, i) => console.log(x)); for (const x of xs) { console.log(x); } Detailed examples are below. 1. Sequential for loop: var myStringArray = [“Hello”,”World”]; var arrayLength = myStringArray.length; for (var i = 0; i < arrayLength; i++) { console.log(myStringArray[i]); //Do … Read more

bahis casinocanlı casino siteleritürkçe altyazılı pornocanlı bahis casino