Questions tagged [arrays]

An array is an ordered data structure consisting of a collection of elements (values or variables), each identified by one or multiple indexes. When asking about specific variants of arrays, use these related tags instead: [vector], [arraylist], [matrix]. When using this tag, in a question which is ...

986
votes
27answers
1.4m views

“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP

I'm running a PHP script, and continue to receive errors like:Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php on line 10Notice: Undefined index: my_index C:\wamp\...
665
votes
20answers
402k views

Access / process (nested) objects, arrays or JSON

I have a nested data structure containing objects and arrays. How can I extract the information, i.e. access a specific or multiple values (or keys)?For example:var data={code: 42,...
214
votes
21answers
480k views

What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

What does ArrayIndexOutOfBoundsException mean and how do I get rid of it? Here is a code sample that triggers the exception:String[] name={"tom", "dick", "harry"};for(int i=0; i<=name....
1578
votes
25answers
725k views

Why is using “for…in” with array iteration a bad idea?

I've been told not to use for...in with arrays in JavaScript. Why not?
258
votes
9answers
50k views

How can I sort arrays and data in PHP?

This question is intended as a reference for questions about sorting arrays in PHP. It is easy to think that your particular case is unique, and worthy of a new question, but most are actually minor ...
1600
votes
30answers
1.8m views

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

In Java, arrays don't override toString(), so if you try to print one directly, you get the className + @ + the hex of the hashCode of the array, as defined by Object.toString():int[] intArray=new ...
921
votes
43answers
523k views

How to randomize (shuffle) a JavaScript array?

I have an array like this:var arr1=["a", "b", "c", "d"];How can I randomize / shuffle it?
431
votes
5answers
116k views

How do I use arrays in C++?

C++ inherited arrays from C where they are used virtually everywhere. C++ provides abstractions that are easier to use and less error-prone (std::vector<T> since C++98 and std::array<T, n> ...
294
votes
8answers
39k views

What is array decaying?

What is decaying of an array? Is there any relation to array pointers?
45
votes
4answers
28k views

How can I access an array/object?

I have the following array and when I do print_r(array_values($get_user));, I get:Array ([0]=> 10499478683521864[1]=> 07/22/1983[2]=> email@saya.com...
1934
votes
39answers
979k views

Sort array of objects by string property value

I have an array of JavaScript objects:var objs=[ { first_nom: 'Lazslo', last_nom: 'Jamf' },{ first_nom: 'Pig', last_nom: 'Bodine' },{ first_nom: 'Pirate', last_nom: '...
3912
votes
28answers
3.4m views

For-each over an array in JavaScript?

How can I loop through all the entries in an array using JavaScript?I thought it was something like this:forEach(instance in theArray)Where theArray is my array, but this seems to be incorrect.
911
votes
29answers
630k views

How to create a generic array in Java?

Due to the implementation of Java generics, you can't have code like this: public class GenSet<E> {private E a[];public GenSet() {a=new E[INITIAL_ARRAY_LENGTH]; // error: ...
913
votes
9answers
632k views

Sort Multi-dimensional Array by Value

How can I sort this array by the value of the "order" key? Even though the values are currently sequential, they will not always be.Array([0]=> Array([hashtag]=> ...
253
votes
14answers
294k views

How to find the 'sizeof' (a pointer pointing to an array)?

First off, here is some code:int main() {int days[]={1,2,3,4,5};int *ptr=days;printf("%u\n", sizeof(days));printf("%u\n", sizeof(ptr));return 0;}Is there a way to ...
3322
votes
42answers
2.2m views

How do I check if an array includes an object in JavaScript?

What is the most concise and efficient way to find out if a JavaScript array contains an object?This is the only way I know to do it:function contains(a, obj) {for (var i=0; i < a.length;...
54
votes
7answers
71k views

Arrays, linked lists and other data structures in cmd.exe (batch) script

I was playing with cmd.exe, but in its help I didn't find any info, how to define arrays.I have found, how to define simple variables:set a=10echo %a%But, I want to create arrays, linked list ...
1057
votes
25answers
763k views

Sorting an array of JavaScript objects

I read the following objects using Ajax and stored them in an array:var homes=[{"h_id": "3","city": "Dallas","state": "TX","zip": "75201","price": ...
701
votes
49answers
594k views

How to compare arrays in JavaScript?

I'd like to compare two arrays... ideally, efficiently. Nothing fancy, just true if they are identical, and false if not. Not surprisingly, the comparison operator doesn't seem to work.var a1=[1,2,...
165
votes
9answers
51k views

Is an array name a pointer?

Is an array's name a pointer in C?If not, what is the difference between an array's name and a pointer variable?
1455
votes
17answers
85k views

With arrays, why is it the case that a[5]==5[a]?

As Joel points out in Stack Overflow podcast #34, in C Programming Language (aka: K & R), there is mention of this property of arrays in C: a[5]==5[a]Joel says that it's because of pointer ...
2508
votes
38answers
3.0m views

Loop through an array in JavaScript

In Java you can use a for loop to traverse objects in an array as follows:String[] myStringArray={"Hello", "World"};for (String s : myStringArray){// Do something}Can you do the same in ...
1153
votes
39answers
696k views

How do you convert a byte array to a hexadecimal string, and vice versa?

How can you convert a byte array to a hexadecimal string, and vice versa?
6419
votes
69answers
5.1m views

How do I remove a particular element from an array in JavaScript?

I have an array of integers, and I'm using the .push() method to add elements to it.Is there a simple way to remove a specific element from an array? The equivalent of something like array.remove(...
929
votes
54answers
927k views

Remove duplicate values from JS array [duplicate]

I have a very simple JavaScript array that may or may not contain duplicates.var names=["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];I need to remove the duplicates and put the unique ...
93
votes
6answers
16k views

Is Chrome's JavaScript console lazy about evaluating arrays?

I'll start with the code:var s=["hi"];console.log(s);s[0]="bye";console.log(s);Simple, right? In response to this, Firebug says:["hi"]["bye"]Wonderful, but Chrome's JavaScript console ...
517
votes
21answers
177k views

Can I bind an array to an IN() condition?

I'm curious to know if it's possible to bind an array of values to a placeholder using PDO. The use case here is attempting to pass an array of values for use with an IN() condition. I'd like to ...
1413
votes
28answers
678k views

Copy array by value

When copying an array in JavaScript to another array:var arr1=['a','b','c'];var arr2=arr1;arr2.push('d'); //Now, arr1=['a','b','c','d']I realized that arr2 refers to the same array as ...
253
votes
13answers
117k views

Why aren't variable-length arrays part of the C++ standard?

I haven't used C very much in the last few years. When I read this question today I came across some C syntax which I wasn't familiar with.Apparently in C99 the following syntax is valid:void foo(...
145
votes
11answers
76k views

How to Update Multiple Array Elements in mongodb

I have a Mongo document which holds an array of elements.I'd like to reset the .handled attribute of all objects in the array where .profile=XX.The document is in the following form: {"...
1165
votes
29answers
1.1m views

Find object by id in an array of JavaScript objects

I've got an array:myArray=[{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}, etc.]I'm unable to change the structure of the array. I'm being passed an id of 45, and I want to get 'bar' for that ...
876
votes
60answers
831k views

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

I have an array of numbers that I need to make sure are unique. I found the code snippet below on the internet and it works great until the array has a zero in it. I found this other script here on SO ...
424
votes
21answers
716k views

How do I declare a 2d array in C++ using new?

How do i declare a 2d array using new?Like, for a "normal" array I would:int* ary=new int[Size]butint** ary=new int[sizeY][sizeX]a) doesn't work/compile and b) doesn't accomplish what:...
132
votes
17answers
87k views

Accessing an array out of bounds gives no error, why?

I am assigning values in a C++ program out of the bounds like this:#include <iostream>using namespace std;int main(){int array[2];array[0]=1;array[1]=2;array[3]=3;...
1685
votes
19answers
3.8m views

How do I declare and initialize an array in Java?

How do I declare and initialize an array in Java?
759
votes
63answers
424k views

Merge/flatten an array of arrays in JavaScript?

I have a JavaScript array like:[["$6"], ["$12"], ["$25"], ["$25"], ["$18"], ["$22"], ["$10"]]How would I go about merging the separate inner arrays into one like:["$6", "$12", "$25", ...]
1017
votes
62answers
768k views

How to merge two arrays in JavaScript and de-duplicate items

I have two JavaScript arrays:var array1=["Vijendra","Singh"];var array2=["Singh", "Shakya"];I want the output to be:var array3=["Vijendra","Singh","Shakya"];The output array should have ...
326
votes
44answers
329k views

Split array into chunks

Let's say that I have an Javascript array looking as following: ["Element 1","Element 2","Element 3",...]; // with close to a hundred elements.What approach would be appropriate to chunk (split) ...
187
votes
25answers
356k views

Random shuffling of an array

I need to randomly shuffle the following Array:int[] solutionArray={1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1};Is there any function to do that?
255
votes
17answers
191k views

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

How can I remove duplicate values from a multi-dimensional array in PHP?Example array:Array([0]=> Array([0]=> abc[1]=> def)[1]=> Array(...
159
votes
6answers
64k views

How come an array's address is equal to its value in C?

In the following bit of code, pointer values and pointer addresses differ as expected.But array values and addresses don't!How can this be?Outputmy_array=0022FF00&my_array=0022FF00...
195
votes
24answers
174k views

How to Flatten a Multidimensional Array?

Is it possible, in PHP, to flatten a (bi/multi)dimensional array without using recursion or references?I'm only interested in the values so the keys can be ignored, I'm thinking in the lines of ...
38
votes
2answers
3k views

Correctly allocating multi-dimensional arrays

The intent of this question is to provide a reference about how to correctly allocate multi-dimensional arrays dynamically in C. This is a topic often misunderstood and poorly explained even in some C ...
95
votes
13answers
69k views

Why isn't the size of an array parameter the same as within main?

Why isn't the size of an array sent as a parameter the same as within main?#include <stdio.h>void PrintSize(int p_someArray[10]);int main () {int myArray[10];printf("%d\n", ...
515
votes
22answers
565k views

How to convert a byte array to a hex string in Java?

I have a byte array filled with hex numbers and printing it the easy way is pretty pointless because there are many unprintable elements. What I need is the exact hexcode in the form of: 3a5f771c
810
votes
19answers
1.6m views

How to initialize all members of an array to the same value?

I have a large array in C (not C++ if that makes a difference). I want to initialize all members to the same value. I could swear I once knew a simple way to do this. I could use memset() in my case, ...
234
votes
12answers
475k views

Passing a 2D array to a C++ function

I have a function which I want to take, as a parameter, a 2D array of variable size. So far I have this: void myFunction(double** myArray){myArray[x][y]=5;etc...}And I have ...
487
votes
58answers
361k views

How to get the difference between two arrays in Javascript?

Is there a way to return the difference between two arrays in JavaScript?For example:var a1=['a', 'b'];var a2=['a', 'b', 'c', 'd'];// need ["c", "d"]Any advice greatly appreciated.
14
votes
9answers
20k views

What is a NumberFormatException and how can I fix it?

Error Message:Exception in thread "main" java.lang.NumberFormatException: For input string: "Ace of Clubs"at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)at ...
560
votes
15answers
313k views

How to sort an array of integers correctly

Trying to get the highest and lowest value from an array that I know will contain only integers seems to be harder than I thought.var numArray=[140000, 104, 99];numArray=numArray.sort();...

153050per page
angop.ao, elkhabar.com, noa.al, afghanpaper.com, bbc.com, time.com, cdc.gov, nih.gov, xnxx.com, github.com,