All Questions

Tagged with
Filter by
Sorted by
Tagged with
2122 votes
26 answers
1.3m views

How do I check if a variable is an array in JavaScript?

How do I check if a variable is an array in JavaScript? if (variable.constructor == Array)
Andy McCluggage's user avatar
1297 votes
25 answers
2.0m views

Converting array to list in Java

How do I convert an array to a list in Java? I used the Arrays.asList() but the behavior (and signature) somehow changed from Java SE 1.4.2 (docs now in archive) to 8 and most snippets I found on the ...
Alexandru's user avatar
  • 25.5k
756 votes
11 answers
1.4m views

Convert list to array in Java [duplicate]

How can I convert a List to an Array in Java? Check the code below: ArrayList<Tienda> tiendas; List<Tienda> tiendasList; tiendas = new ArrayList<Tienda>(); Resources res = this....
colymore's user avatar
  • 12.1k
755 votes
16 answers
418k views

Array versus List<T>: When to use which?

MyClass[] array; List<MyClass> list; What are the scenarios when one is preferable over the other? And why?
G S's user avatar
  • 36.1k
618 votes
17 answers
2.4m views

How do I declare an array in Python?

How do I declare an array in Python?
user avatar
568 votes
7 answers
249k views

What are the advantages of NumPy over regular Python lists?

What are the advantages of NumPy over regular Python lists? I have approximately 100 financial markets series, and I am going to create a cube array of 100x100x100 = 1 million cells. I will be ...
Thomas Browne's user avatar
482 votes
9 answers
1.3m views

How to declare and add items to an array in Python

I'm trying to add items to an array in Python. I run array = {} Then, I try to add something to this array by doing: array.append(valueToBeInserted) There doesn't seem to be an .append method for ...
AkshaiShah's user avatar
  • 5,809
452 votes
11 answers
376k views

Python list vs. array – when to use?

If you are creating a 1d array, you can implement it as a list, or else use the 'array' module in the standard library. I have always used lists for 1d arrays. What is the reason or circumstance ...
Corey Goldberg's user avatar
403 votes
32 answers
347k views

Array or List in Java. Which is faster?

I have to keep thousands of strings in memory to be accessed serially in Java. Should I store them in an array or should I use some kind of List ? Since arrays keep all the data in a contiguous chunk ...
euphoria83's user avatar
352 votes
4 answers
151k views

Difference between List and Array types in Kotlin

What is the difference between List and Array types? It seems can make same operations with them (loops, filter expression, etc..), is there any difference in behavior or usage? val names1 = listOf("...
Daniel Hári's user avatar
  • 7,464
340 votes
11 answers
668k views

Conversion of System.Array to List

Last night I had dream that the following was impossible. But in the same dream, someone from SO told me otherwise. Hence I would like to know if it it possible to convert System.Array to List Array ...
user193276's user avatar
  • 3,971
337 votes
5 answers
247k views

Is the order of elements in a JSON list preserved?

I've noticed the order of elements in a JSON object not being the original order. What about the elements of JSON lists? Is their order maintained?
user437899's user avatar
  • 9,029
306 votes
9 answers
162k views

How is Python's List Implemented?

Is it a linked list, an array? I searched around and only found people guessing. My C knowledge isn't good enough to look at the source code.
Greg's user avatar
  • 46.1k
298 votes
11 answers
1.0m views

Initialising an array of fixed size in Python [duplicate]

I would like to know how i can initialize an array(or list), yet to be populated with values, to have a defined size. For example in C: int x[5]; /* declared without adding elements*/ How do I do ...
kbb's user avatar
  • 3,273
285 votes
9 answers
690k views

Initializing a list to a known number of elements in Python [duplicate]

Right now I am using a list, and was expecting something like: verts = list (1000) Should I use array instead?
Joan Venge's user avatar
  • 323k
260 votes
16 answers
268k views

When to use a linked list over an array/array list?

I use a lot of lists and arrays but I have yet to come across a scenario in which the array list couldn't be used just as easily as, if not easier than, the linked list. I was hoping someone could ...
faceless1_14's user avatar
  • 7,522
256 votes
15 answers
257k views

Performance of Arrays vs. Lists

Say you need to have a list/array of integers which you need iterate frequently, and I mean extremely often. The reasons may vary, but say it's in the heart of the inner most loop of a high volume ...
Boaz's user avatar
  • 25.7k
225 votes
34 answers
280k views

Build tree array from flat array in javascript

I have a complex json file that I have to handle with javascript to make it hierarchical, in order to later build a tree. Every entry of the json has : id : a unique id, parentId : the id of the ...
Franck's user avatar
  • 2,489
223 votes
7 answers
1.2m views

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

I am trying to read the lines of a text file into a list or array in python. I just need to be able to individually access any item in the list or array after it is created. The text file is ...
user2037744's user avatar
  • 2,255
218 votes
6 answers
323k views

How do you concatenate Lists in C#?

If I have: List<string> myList1; List<string> myList2; myList1 = getMeAList(); // Checked myList1, it contains 4 strings myList2 = getMeAnotherList(); // Checked myList2, it contains 6 ...
Matt's user avatar
  • 5,339
181 votes
10 answers
81k views

Is there a performance impact when calling ToList()?

When using ToList(), is there a performance impact that needs to be considered? I was writing a query to retrieve files from a directory, which is the query: string[] imageArray = Directory.GetFiles(...
Cody's user avatar
  • 8,816
171 votes
10 answers
10k views

Is there a way to measure how sorted a list is?

Is there is a way to measure how sorted a list is? I mean, it's not about knowing if a list is sorted or not (boolean), but something like a ratio of "sortness", something like the coefficient of ...
Josell's user avatar
  • 1,934
163 votes
8 answers
34k views

Why array implements IList?

See the definition of System.Array class public abstract class Array : IList, ... Theoretically, I should be able to write this bit and be happy int[] list = new int[] {}; IList iList = (IList)list;...
oleksii's user avatar
  • 35.7k
162 votes
3 answers
85k views

Difference between Array and List in scala

In what cases I should use Array(Buffer) and List(Buffer). Only one difference that I know is that arrays are nonvariant and lists are covariant. But what about performance and some other ...
Jeriho's user avatar
  • 7,179
161 votes
6 answers
202k views

How to return a part of an array in Ruby?

With a list in Python I can return a part of it using the following code: foo = [1,2,3,4,5,6] bar = [10,20,30,40,50,60] half = len(foo) / 2 foobar = foo[:half] + bar[half:] Since Ruby does ...
Christian Stade-Schuldt's user avatar
158 votes
8 answers
310k views

How to check if element in groovy array/hash/collection/list?

How do I figure out if an array contains an element? I thought there might be something like [1, 2, 3].includes(1) which would evaluate as true.
banderson623's user avatar
  • 2,748
141 votes
4 answers
358k views

Flatten list of lists [duplicate]

I'm having a problem with square brackets in Python. I wrote a code that produces the following output: [[180.0], [173.8], [164.2], [156.5], [147.2], [138.2]] But I would like to perform some ...
Hellfish's user avatar
  • 1,695
130 votes
5 answers
124k views

Create mutable List from array?

I have an array I'd like to turn into a List, in order to modify the contents of the array. Stack Overflow has plenty of questions/answers that address Arrays.asList() and how it only provides a List ...
ericsoco's user avatar
  • 25.6k
124 votes
7 answers
122k views

Why are lists used infrequently in Go?

Is there a way to create an array/slice in Go without a hard-coded array size? Why is List ignored? In all the languages I've worked with extensively: Delphi, C#, C++, Python - Lists are very ...
Gramps's user avatar
  • 11.2k
120 votes
1 answer
133k views

Convert 2d numpy array into list of lists [duplicate]

I use an external module (libsvm), which does not support numpy arrays, only tuples, lists and dicts. But my data is in a 2d numpy array. How can I convert it the pythonic way, aka without loops. &...
Framester's user avatar
  • 34.3k
109 votes
6 answers
644k views

List to array conversion to use ravel() function

I have a list in python and I want to convert it to an array to be able to use ravel() function.
user2229953's user avatar
  • 1,559
107 votes
6 answers
13k views

How do arrays in C# partially implement IList<T>?

So as you may know, arrays in C# implement IList<T>, among other interfaces. Somehow though, they do this without publicly implementing the Count property of IList<T>! Arrays have only a ...
MgSam's user avatar
  • 12.5k
102 votes
8 answers
63k views

List<T>.Contains() is very slow?

Could anyone explain me why the generics List.Contains() function is so slow? I have a List<long> with about a million numbers, and the code that is constantly checking if there's a specific ...
DSent's user avatar
  • 1,031
102 votes
4 answers
127k views

Convert an array into an ArrayList [duplicate]

I'm having a lot of trouble turning an array into an ArrayList in Java. This is my array right now: Card[] hand = new Card[2]; "hand" holds an array of "Cards". How this would look like as an ...
Saatana's user avatar
  • 1,177
92 votes
8 answers
175k views

Conversion from List<T> to array T[]

Is there a short way of converting a strongly typed List<T> to an Array of the same type, e.g.: List<MyClass> to MyClass[]? By short i mean one method call, or at least shorter than: ...
tehvan's user avatar
  • 10.3k
86 votes
3 answers
225k views

Python Array with String Indices

Is it possible to use strings as indices in an array in python? For example: myArray = [] myArray["john"] = "johns value" myArray["jeff"] = "jeffs value" print myArray["john"]
Petey B's user avatar
  • 11.5k
77 votes
11 answers
47k views

Arrays.asList() not working as it should?

I have a float[] and i would like to get a list with the same elements. I could do the ugly thing of adding them one by one but i wanted to use the Arrays.asList method. There is a problem though. ...
Savvas Dalkitsis's user avatar
77 votes
10 answers
112k views

find out the elements of an arraylist which is not present in another arraylist

I have to find a best way to find out that elements which is not presented in the second arraylist. suppose Arraylist a,b, Arraylist a={1,2,3,4,5}; Arraylist b={2,3,4}; So basically what I want ...
arvin_codeHunk's user avatar
77 votes
17 answers
31k views

Easiest way to Rotate a List in c#

Lists say I have a list List<int> {1,2,3,4,5} Rotate means: => {2,3,4,5,1} => {3,4,5,1,2} => {4,5,1,2,3} Maybe rotate is not the best word for this, but hope you understand what I ...
Eric Yin's user avatar
  • 8,853
76 votes
8 answers
240k views

How can I create an array/list of dictionaries in python?

I have a dictionary as follows: {'A':0,'C':0,'G':0,'T':0} I want to create an array with many dictionaries in it, as follows: [{'A':0,'C':0,'G':0,'T':0},{'A':0,'C':0,'G':0,'T':0},{'A':0,'C':0,'G':0,...
Adrian Randall's user avatar
74 votes
3 answers
153k views

how to flatten a 2D list to 1D without using numpy? [duplicate]

I have a list looks like this: [[1,2,3],[1,2],[1,4,5,6,7]] and I want to flatten it into [1,2,3,1,2,1,4,5,6,7] is there a light weight function to do this without using numpy?
wakeupbuddy's user avatar
  • 1,001
71 votes
3 answers
54k views

JavaScript filter that stops at the first result

Is there a mechanism in JavaScript (without having to write my own) similar to filter. Instead of returning all the filtered elements of a collection though, it only returns the first one. Of course I ...
at.'s user avatar
  • 51.8k
71 votes
10 answers
136k views

How to create a numpy array of lists?

I want to create a numpy array in which each element must be a list, so later I can append new elements to each. I have looked on google and here on stack overflow already, yet it seems nowhere to be ...
Ricardo Silveira's user avatar
70 votes
7 answers
98k views

Assign value to an individual cell in a two dimensional python array [duplicate]

Let's say I have the following empty two dimensional array in Python: q = [[None]*5]*4 I want to assign a value of 5 to the first row in the first column of q. Instinctively, I do the following: q[...
Ben McCormack's user avatar
70 votes
5 answers
15k views

How to compare equality of lists of arrays with modern Java?

I have two lists of arrays. How do I easily compare equality of these with Java 8 and its features, without using external libraries? I am looking for a "better" (higher-level, shorter, more ...
hyde's user avatar
  • 61.8k
69 votes
8 answers
430k views

Working with a List of Lists in Java

I'm trying to read a CSV file into a list of lists (of strings), pass it around for getting some data from a database, build a new list of lists of new data, then pass that list of lists so it can be ...
user avatar
69 votes
7 answers
98k views

What is the shortest way to initialize List of strings in java?

I am searching for the shortest way (in code) to initialize list of strings and array of strings, i.e. list/array containing "s1", "s2", "s3" string elements.
Vladimir's user avatar
  • 12.9k
67 votes
7 answers
54k views

Reserve memory for list in Python?

When programming in Python, is it possible to reserve memory for a list that will be populated with a known number of items, so that the list will not be reallocated several times while building it? ...
dsimcha's user avatar
  • 68k
65 votes
7 answers
185k views

An array of List in c#

I want to have an array of Lists. In c++ I do like: List<int> a[100]; which is an array of 100 Lists. each list can contain many elements. I don't know how to do this in c#. Can anyone help me?...
orezvani's user avatar
  • 3,635
65 votes
11 answers
20k views

Can I create a "view" on a Python list?

I have a large list l. I want to create a view from element 4 to 6. I can do it with sequence slice. >>> l = range(10) >>> lv = l[3:6] >>> lv [3, 4, 5] However lv is a copy ...
Wai Yip Tung's user avatar
  • 18.4k

1
2 3 4 5
165