15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

554<br />

Data Structures<br />

Creating a C# structure<br />

Unlike Java, C# offers a structure, which acts like a variable that typically<br />

holds two or more variables. To create a structure, use the struct keyword<br />

as follows:<br />

struct name {<br />

public datatype variable;<br />

};<br />

The name of a structure can be any descriptive name, such as people2get<br />

or my_relatives. Inside a structure, you must declare one or more variables.<br />

A typical structure might look like this:<br />

struct MyGrades {<br />

public char grade;<br />

public int class_number;<br />

};<br />

After defining a structure, you can declare a variable to represent that structure,<br />

such as<br />

struct geology = MyGrades();<br />

After declaring a variable as a structure, you can store data in the individual<br />

fields of a structure like this:<br />

struct geology = MyGrades();<br />

geology.grade = “A”;<br />

geology.class_number = 302;<br />

Creating an array<br />

Arrays in Java/C# are known as zero-based arrays, which means that the first<br />

element of the array is located at index number 0, the second element of the<br />

array is located at index number 1, and so on.<br />

To create an array in Java, declare its data type and size, such as<br />

datatype [] arrayname = new datatype[arraysize];<br />

The array name can be any descriptive name. The array size defines how<br />

many items the array can hold. To create an array that can hold ten integers,<br />

you could define an array like this:<br />

int [] mynumbers = new int[10];

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!