Archive

Posts Tagged ‘Arrays’

Javascript Arrays

May 10, 2010 Leave a comment

So here is how to arrays in Javascript. I have not gone into depth on all the array methods in this post like the last on C Sharp. I am going to check out the functions provided by JQuery in terms of providing foreach and the like.

I do like how arrays in JS are dynamic and you can add elements to the end.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Examples 2</title>
    <script type="text/javascript">

        function addElementToArray(farray,value){ // Arrays are passed by reference
            farray[farray.length] = value;
        }

        function printLastItem(farray){
            document.writeln(farray[farray.length - 1]);
        }
        var items1 = new Array(10); // Declare array with 10 elements
        var items2 = []; // Create empty array with zero element equal to undefined

        // Add elements to the arrays and then print them to the browser
        for(i = 0; i < 10; i++){
            items1[i] = i;
            items2[i] = i;
        }

        for(j = 0; j < items1.length; j++){
            document.writeln('items1 value at position ' + j + ' is ' + items1[j] + '<br/>');
        }

        for(k = 0; k < items2.length; k++){
            document.writeln('items2 value at position ' + k + ' is ' + items1[k] + '<br/>');
        }

        addElementToArray(items1,'test1');
        printLastItem(items1);
        addElementToArray(items1,'test2');
        printLastItem(items1);
        
    </script>
</head>
<body>

</body>
</html>

Blair…

Advertisements

CSharp Arrays

May 10, 2010 Leave a comment

This follows on from my last post. This code was created as a snippet using the framework posted before and highlights pretty much most of the stuff you can do with arrays in .NET.

I am also going to start comparing Javascript arrays to .NET arrays as I am currently learning / educating myself with Javascript.

using System;
namespace CSharpInDepth
{
	public class ArrayExamples : IRunnable
	{
		public ArrayExamples()
		{
		}
		
		public void RunExamples ()
		{
			Console.WriteLine ("Examples of declaring and using arrays");
			
			int[] values1 = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
			
			Console.WriteLine ("Printing array numbers");
			Array.ForEach (values1, x => Console.WriteLine ("Value: {0}", x));
			
			Console.WriteLine ("Number of dimensions: {0}", values1.Rank);
			Console.WriteLine ("Number of elements: ", values1.Length);
			
			Console.WriteLine ("Reversing array");
			Array.Reverse (values1);
			Console.WriteLine ("Printing array numbers");
			Array.ForEach (values1, x => Console.WriteLine ("Value: {0}", x));
			
			Console.WriteLine ("Sorting the elements");
			Array.Sort (values1);
			Console.WriteLine ("Printing array numbers");
			Array.ForEach (values1, x => Console.WriteLine ("Value: {0}", x));
			
			Console.WriteLine("Number 10 exists in the array is: {0}",Array.Exists<int>(values1, x => x == 10));
			int value = Array.Find<int>(values1, x => x.Equals(10));
			Console.WriteLine("The item found in the array matching the predicate is: {0}", value);
			
			Console.WriteLine("Finding the number 10 in the array");
			int index = Array.BinarySearch(values1,10);
			Console.WriteLine("Found at index position: {0}",index);
			
			int[,] values2 = new[,]{{1,2,3,4,5},{6,7,8,9,10}};
			Console.WriteLine("Values 2 rank is: {0}", values2.Rank);
			Console.WriteLine("Dimension 1: {0}", values2.GetLength(0));
			Console.WriteLine("Dimension 2: {0}", values2.GetLength(1));
			
			int[] values3 = new int[values1.Length - 1]; // Declare array reference for copying
			Array.Copy(values1,values3,values1.Length - 1);
			Console.WriteLine ("Printing new array numbers");
			Array.ForEach (values1, x => Console.WriteLine ("Value: {0}", x));
			
			Console.WriteLine("Clearing the array");
			Array.Clear(values1,0,values1.Length);
			Console.WriteLine("Count of the items in the array after clearing is: {0}", values1.Length);
			Console.WriteLine ("Printing cleared array numbers");
			Array.ForEach (values1, x => Console.WriteLine ("Value: {0}", x));
			
			
		}
	}
}

Blair….