Tag Archives: arrays
Example for building a unit matrix in Javascript
Array.identity = function (n) { var i, mat = Array.matrix(n, n, 0); for (i = 0; i < n; i += 1) { mat[i][i] = 1; } return mat; }; myMatrix = Array.identity(4); document.writeln(myMatrix[3][3]); // 1
An array of strings in C#
The array is a special kind of object that can hold multiple items, without needing to declare a field for each individual item. string[ ] eventNames = { "Swing Dancing", "Night Swing", "Formula 1 Grand Prix", "Swing Dance" };
Use orderby to retrieve the values in an int array in ascending order in LINQ
using System; using System.Linq; class OrderbyDemo { static void Main() { int[] nums = { 10, -19, 4, 7, 2, -5, 0 }; // Create a query that obtains the values in sorted order. var posNums = from n in … Continue reading
Example of using IEnumerable on an integer array
// create an integer array int[] myArray = new int[] {1, 2, 3, 4, 5 }; IEnumerable<int> intArray = myArray.Select(i => i); //LINQ query expression var query = from num in intArray where num >= 3 select num; foreach (var … Continue reading
Obtaining an Array from an ArrayList in C#
To obtain an actual array that contains the contents of the list we call the ToArray() method on the arraylist. /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill */ using System; using System.Collections; class ArrayListToArray { static void … Continue reading
Example a two-dimensional array in C#
using System; class TwoD { public static void Main() { int t, i; int[,] table = new int[3, 4]; for(t=0; t < 3; ++t) { for(i=0; i < 4; ++i) { table[t,i] = (t*4)+i+1; Console.Write(table[t,i] + " "); … Continue reading
Compute the average of a set of values from an array
using System; class Average { public static void Main() { int[] nums = new int[10]; int avg = 0; nums[0] = 99; nums[1] = 10; nums[2] = 100; nums[3] = 18; nums[4] = 78; nums[5] = 23; nums[6] … Continue reading
One-dimensional array code sample
using System; class ArrayDemo { public static void Main() { int[] sample = new int[10]; int i; for(i = 0; i < 10; i = i+1) sample[i] = i; for(i = 0; i < 10; i = … Continue reading
Actionscript arrays
var myArray:Array = new Array(); myArray.push(1); trace(myArray) // 1 appears in the Output panel myArray.push(2); // the array now has two items: 1, 2 trace(myArray.pop()); // the pop() method removes the last item, displaying its value of 2 trace(myArray) // … Continue reading
Dynamically adjust the size of an array in c#
Generally, after we create an array we cannot make any adjustment of its length, but the array class provides a static method named CreateInstance to create a dynamic array, of course, we can use this to dynamically adjust its features … Continue reading
Convert a bool to a byte array and display
Example to convert a bool to a byte array and display: byte[] b = null; b = BitConverter.GetBytes(true); Console.WriteLine(BitConverter.ToString(b));
Convert a decimal to a byte array in c#
The following code shows how to convert a decimal to a byte array using a MemoryStream and a BinaryWriter. // Create a byte array from a decimal public static byte[] DecimalToByteArray (decimal src) { // Create a MemoryStream as … Continue reading
Convert a byte array to a decimal using c#
To convert a byte array to a decimal, we can use a BinaryReader to read from the MemoryStream. The following is an example: // Create a decimal from a byte array public static decimal ByteArrayToDecimal (byte[] src) { // … Continue reading
Using loops to display array data in codeigniter views
In this example we will see how to use loops to display multidimensional data in codeigniter views. Controller: <?php class SimpleController extends Controller { function index() { $data[’my_list’] = array("do this", "clean up", "do that"); $this->load->view(’index’, $data); } } ?> … Continue reading
Divide one array into two or more arrays in Javascript
To divide an array into two pieces we can use the splice() method on the original array in Javascript. The splice( ) method requires two parameters that signify the zero-based index of the first item, and the number of items … Continue reading
Combine arrays using Javascript
To join two or more separate arrays into one larger array, we can use the concat() method of the array object. It takes a reference to the second array(s) as the parameter. Example: var combinedArray = oneArray.concat(anotherArray); Multiple arrays can … Continue reading
Multidimensional array nested looping in Javascript
The following is an example of nested for loops on multidimensional arrays in Javascript. The script will reference each item and accumulate the numeric values from all entries of the two-dimensional array. var total = 0; var i, j; for … Continue reading
Clear all textboxes using Javascript
The following example shows how to empty all of the text boxes on a page, even if the page contains multiple forms. The index on an array is used to set properties on the target element. var allInputs = document.getElementsByTagName("input"); … Continue reading
Loop through an array and read values in Javascript
To loop through all entries of an array and read their values, we can use a for loop to build an incrementing index counter, limited by the length of the array. Example: var myArray = ["Mani", "Kaka", "Biryani", "Mad"]; for … Continue reading
Convert array to string using Javascript
The join() method joins all elements of an array into a string, and returns the string. The elements will be separated by a specified separator. The default separator is comma (,). Declaring individual array var oneArray = new Array(2300, 3105, … Continue reading