⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 array.jc

📁 The JILRunOnly project is a simple command-line application written in ANSI-C that is intended to de
💻 JC
字号:

/*
 *	array.jc
 *
 *	This script tests / demonstrates the built-in array.
 *	The built-in array is a simple, one dimensional array, which can grow
 *	dynamically, based on the index used to read or write the array.
 *	The good thing about this is, that you can never access an array out of
 *	boundary (unless you use a negative index!). However, the downside is,
 *	that a programming error (specified index unintentionally a huge number)
 *	can easily result in the allocation of gigabytes of memory, so arrays
 *	should be used with caution.
 *	The arrays element type is implicitly 'var', which means it is typeless
 *	and you can store any data type in it, and even mix them.
 *	To create a multi-dimensional array, you can put an array into an array,
 *	into an array, and so forth, up to the number of dimensions needed.
 *	This example creates a two-dimensional array.
 *
 *	Note that the reference / non-reference type of the elements of an array
 *	depends on whether the array variable used is a reference or non-reference.
 *	That means, if the array is accessed through a reference, then also the
 *	arrays elements are treated as references. If the array is accessed by a
 *	normal variable, then the arrays elements are also treated as normal
 *	variables.
 *
 *	Specialities:
 *	- The "+" operator can be used with arrays to add a new element to the
 *	  end of the array.
 *	- The "length" accessor can be used to get the arrays current number of
 *	  elements. 
 *	- The typeof() operator can be used to determine the type of an element in
 *    the array.
 */

/*
 *	import stdlib, so we can print
 */

import stdlib;

/*
 *	function main
 */

function string main()
{
	// create a Foo instance
	Foo foo = new Foo();

	// set some members
	foo.Set(0, 0, "Hello");
	foo.Set(1, 0, "World");
	foo.Set(0, 1, "This");
	foo.Set(1, 1, "is" );
	foo.Set(0, 2, "a" );
	foo.Set(0, 3, "test!");
	foo.Set(0, 4, 76 );
	foo.Set(1, 4, "string");
	foo.Set(0, 5, "hi");
	foo.Set(1, 5, 3.141);
	foo.Set(2, 5, 0.5);

	// print it all out
	foo.Print();

	return "";
}

/*
 *	class Foo
 *
 *	Our test subject
 */

class Foo
{
	/*
	 *	Foo constructor
	 */
	method Foo()
	{
		// create an empty array
		m_array = new array;
		// fill it with 10 empty arrays
		for( long i = 0; i < 10; i++ )
		{
			m_array[i] = new array;
		}
	}

	/*
	 *	Set an array element
	 */
	method Set(long x, long y, var value)
	{
		m_array[y][x] = value;
	}

	/*
	 *	Print the array elements
	 */
	method Print()
	{
		// loop over all elements in m_array
		for( long i = 0; i < m_array.length; i++ )
		{
			// if the element is an array
			if( typeof(m_array[i]) == typeof(array) )
			{
				// print all elements of that array
				stdlib::Print( i );
				stdlib::Print( ": { " );
				for( long j = 0; j < m_array[i].length; j++ )
				{
					stdlib::Print( m_array[i][j] );
					stdlib::Print( "," );
				}
				stdlib::Print( " }\n" );
			}
			else	// the item is not an array
			{
				// print the item
				stdlib::Print( i );
				stdlib::Print( ": " );
				stdlib::Print( m_array[i] );
				stdlib::Print( "\n" );
			}
		}
	}

	// our array
	array m_array;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -