arrays

Avoiding array duplication

According to MSDN: Array usage guidelines: Array Valued Properties You should use collections to avoid code inefficiencies. In the following code example, each call to the myObj property creates a copy of the array. As a result, 2n+1 copies of the array will be created in the following loop. [Visual Basic] Dim i As Integer For i...

reading a text file into an array in c

What would be the most efficient method of reading a text file into a dynamic one-dimensional array? reallocing after every read char seems silly, reallocing after every read line doesn't seem much better. I would like to read the entire file into the array. How would you do it? ...

Ruby: How to find item in array which has the most occurrences?

[1, 1, 1, 2, 3] => 1 ['cat', 'dog', 'snake', 'dog'] => dog ...

Cleanest way to create a Hash from an Array

I seem to run into this very often. I need to build a Hash from an array using an attribute of each object in the array as the key. Lets say I need a hash of example uses ActiveRecord objecs keyed by their ids Common way: ary = [collection of ActiveRecord objects] hash = ary.inject({}) {|hash, obj| hash[obj.id] = obj } Another Way: ...

When to use ArrayList over array[] in c#?

I often use an ArrayList instead of a 'normal' array[]. I feel as if I am cheating (or being lazy) when I use an ArrayList, when is it okay to use an ArrayList over an array? ...

C# - Array that can be resized fast

I'm looking for a kind of array data-type that can easily have items added, without a performance hit. System.Array - Redim Preserve copies entire RAM from old to new, as slow as amount of existing elements System.Collections.ArrayList - good enough? System.Collections.IList - good enough? ...

Parsing data from txt file in J2ME

Basically I'm creating an indoor navigation system in J2ME. I've put the location details in a .txt file i.e. Locations names and their coordinates. Edges with respective start node and end node as well as the weight (length of the node). I put both details in the same file so users dont have to download multiple files to get their ma...

Sorting an array of an array of objects in PHP by key value

Basically I have a setup like the following: Array ( [0] => Array ( [0] => stdClass Object ( [nid] => 1 [title] => title1 [uid] => 1 [parent] => 0 [weight] => -15 [name] => name1 [value] => 0 ) [1] => stdClass Object ( [nid] => 2 [title] => title2 [uid] => 1 [parent] => 0 [weight] => -7 [name] => name2 [value] => 100 ) ...

Best way to combine 2 or more byte arrays in C#

I have 3 byte arrays in C# that I need to combine into one. What would be the most efficient method to complete this task? ...

PHP: Apply a function to multiple variables without using an array.

I have a function (for ease, I'll just use count()) that I want to apply to maybe 4-5 different variables. Right now, I am doing this: $a = count($a); $b = count($b); $c = count($c); $d = count($d); Is there a better way? I know arrays can use the array_map function, but I want the values to remain as separate values, instead of value...

Flash Programming - Anyone know how to work with an array of images?

A friend has asked me to take a Flash 8.0 static image gallery website and make it dynamic. Most articles I've found for dynamic image galleries involve loading the images on request into an existing movieclip. I can't do this because there are some really neat transitions being used requiring both images to be available. I've figured ...

Array over written with last value assigned?

I have a pointer to pointer array. I am assigning each row in the while loop below and the printf inside the while loop shows each is assigned my id number 1-20. After, out side of the while loop I iterate through the array and every element is written with id 20? Any help is greatly appreciated. (FYI- I am using the Template2doc library...

char x[256] vs. char* = malloc(256*sizeof(char));

Someone here recently pointed out to me in a piece of code of mine I am using char* name = malloc(256*sizeof(char)); // more code free(name); I was under the impression that this way of setting up an array was identical to using char name[256]; and that both ways would require the use of free(). Am I wrong and if so could someon...

matching items from two lists (or arrays)

I'm having a problem with my work that hopefully reduces to the following: I have two List<int>s, and I want to see if any of the ints in ListA are equal to any int in ListB. (They can be arrays if that makes life easier, but I think List<> has some built-in magic that might help.) And I'm sure this is a LINQ-friendly problem, but I'm ...

How to deep copy an irregular 2D array

How can I deep copy an irregularly shaped 2D array in Java? Ie. int[][] nums = {{5}, {9,4}, {1,7,8}, {8,3,2,10}} I'm unable to use Arrays.arrayCopy() for some reason (versioning?) Thanks ...

Initialize multidimensional array

Checkbox[,] checkArray = new Checkbox[2, 3]{{checkbox24,checkboxPref1,null}, {checkbox23,checkboxPref2,null}}; // I am getting error . How do I initialize it? ...

How do I find the case-insensitive unique elements of two arrays in Perl?

I have three arrays. @array1 containing filenames @array2 containing filenames @unique which I want to contain the unique items I use the following code to compare the two arrays and output a third array that contains the unique filenames. @test{@array1} = (); @unqiue = grep {!exists $test{$_}} @array2; However the output is case...

place 20 chess pieces on a checkerboard, in random places

An idea I had to solve this, is to make up an buffer of size 8x8, fill it up with pointers to my checkers (all 20 of them), and the rest leave 0 (null), then run a shuffling algorithm on the buffer, and thats it (just read it as a 8x8 array) I was wondering if there's a better way to do this. I need to write it in C#, and my proposal w...

Passing multi-dimensional arrays in C

Hey guys, I am currently trying to learn C and I have come to a problem that I've been unable to solve. Consider: #include <stdio.h> #include <stdlib.h> #include <string.h> #define ELEMENTS 5 void make(char **array, int *array_size) { int i; char *t = "Hello, World!"; array = malloc(ELEMENTS * sizeof(char *)); for ...

How to check if array element is null to avoid NullPointerException in Java

I have a partially nfilled array of objects, and when I iterate through them I tried to check to see whether the selected object is null before I do other stuff with it. However, even the act of checking if it is null seem to through a NullPointerException. array.length will include all null elements as well. How do you go about checking...