C# Arrays- useful methods(no examples)

C# Arrays- useful methods(no examples)

Properties

So properties are used like : arrayName.propertyName

The Rank property will tell you how many dimensions the array has.

The Length property will tell you how many elements are in a array.

Methods

The copy() method creates a copy of the array. The copy is shallow this means that it copies all the elements of the array whether its a reference type or a value type. However, it doesn’t create copies of objects it just copies the reference to that object. (a deep copy would create a copy of that object so the original and the copy would not be referencing the same object)

The Clear(Array array , int index, int length) method will turn the array starting from the index for length elements. So if you put 2 for index and 5 for length, it will set from index 2 to index 6 to the default for their type.

The GetLength(int dimension) method will return the length of the dimension you gave it. So say you are using a three dimensional array and you put a 2 for the parameter, it will give you the length of the third dimension. (it counts dimensions starting from 0 so for a single dimension array you would only be able to find the length of the 0 dimension without a error)

The GetType() method will return the type of a object it is, so if its a int it will return int if its a long it will return long, if the array is of a class you made it will return that classes name.( to clarify the full method is Object.GetType() not Array.GetType(), although a array is technically a object)

The GetValue(…) method has eight different version. Although there are really only 4 kinds, they just specify the indexes of 32 bit integer (Int32 or int) or 64 bit integer (Int64 or long). So I’ll be using int but each instance of the method could have long as the type for all the parameters.(Note: all parameters must be int or long you cant mix and match them within the same method). Also keep in mind that indexes start from zero in a array.

  1. So GetValue( int index1) is for use with single dimension arrays and will give you the value of the element at that index.
  2. GetValue( int index1 , int index2) is for two-dimensional arrays, and will give you the value of a array at index1 in the first dimension and index2 in the second dimension.
  3. GetValue( int index1, int index2, int index3) is for three dimensional arrays.
  4. GetValue(int[] indices) is for multi-dimensioanl arrays, the number of elements in indices should be equal to the number of dimensional the array you are trying to get a value from with element 0 being the index in the first dimension. (I am assuming)

The SetValue( Object value . . ) method basically the same as GetValue but you are setting the values at those indices, so you also need the value parameter which will be the first one.

The IndexOf( . . .) method has 6 different versions. I will be covering three because the other three just seem to be slightly different versions of these three. Also this only works for one dimensional arrays.

  1. IndexOf(Array array, object value) will return the first index that value appears in the array as a int. Everything is a object so don’t worry about the type.
  2. IndexOf (Array array, object value, int startIndex) will return the first index of the value starting from startIndex in the array as a int.
  3. IndexOf (Array array, object value, int startIndex, int count) will return the first index of the value from between startIndex and startIndex+count-1. So if start index is 2 and count is 5 it will be from between indices of 2 and 6.

The LastIndexOf( . . .) method is the same as IndexOf but its just the last index that the value appears at rather than the first.

The Sort( . . . ) method has 17 different version 9 look very similar to the other 8. Of which I will only be going over two because all the others involve a IComparer parameter which I believe is just something telling the method specifically how you want it sorted or it wants you to use another array as a key for the one your sorting.

  1. Sort (Array array, int index, int length) will rearrange the elements from index for length. So say index is 2 length is five the elements from 2 to 6 will be rearranged according to the default, which I believe would be something like alphabetical for strings.(so like if the array was the alphabet the first element would be “A”.
  2. Sort(Array array) will rearrange the entire array according to the default IComparer.
Java Tuple-useful methods

Java Tuple-useful methods

Creating and Converting Tuples

with()- another way of instantiating a tuple. For example:

TupleType<Type1,Type2, etc> tupleName = TupleType.with( Type1Value, Type2Value, etc);

so its a simple way to create a tuple.

You can also do something similar with fromIterable(), fromCollection() and fromArray(), So it would be like this:

TupleType<Type1,Type2, etc> tupleName = TupleType.fromIterable(NameOfIterable);

TupleType<Type1,Type2, etc> tupleName = TupleType.fromArray(NameOfArray);

TupleType<Type1,Type2, etc> tupleName = TupleType.fromCollection(NameOfCollection);

or(only iterable has the ability to do this I believe)

TupleType<Type1,Type2, etc> tupleName = TupleType.fromIterable(NameOfIterable, index#);

The number of elements in the array, list or whatever else should be the same as the iterable (includes lists and arrays), unless you are using the second version of the iterable method(the with index#).

There are also conversion methods of a tuple to something else specifically toArray(),toList() and toString().Which work like:

Object[ ] arrayName = tupleName.toArray();

List<Object> listName = tupleName.toList();

String stringName= tupleName.toString();

I don’t believe you have to use object if all the values in the tuple are of the same datatype, but the type does have to match the type of all the elements in the tuple and Object is the only datatype that guarantees that it will.

Getting and setting Values

You can get a value in a tuple with getValue(), and getValueX(). An example of these are:

dataype variableName= TupleType.getValue#();

dataype variableName= TupleType.getValue(#);

These methods return a value so keep that in mind. The # stands for the index number of the element of the tuple you are getting.

You can set a value in a tuple with the setAtX(), which work similarly:

TupleType<Type1,Type2, etc> modTupleName = tupleName.setValue#(value);

Tuples are immutable so using these methods will return a new tuple so you should use it when modifying a tuple. Again # represents the index number and value represents what you are putting at that index number.

Adding and removing elements

So to add a element to a tuple you would use the add() or addAtX(). They are formatted like:

TupleType<Type1,Type2, etc> addTupleName = tupleName.addAt#(value);

TupleType<Type1,Type2, etc> addTupleName = tupleName.add(value);

So, add() will add the element to the end so it would be the last index. While addAtX() would add the element at the index of #. So, the index of what was added would be #. You can add multiple elements at the same time, but you have to make sure the TupleType matches the total amount of elements that will be in the Tuple. If you are adding a tuple to a tuple its the number of elements from tuple1 + number of elements of tuple2. Remember that these methods create a new tuple.

To remove a element use the removeFromX(). Which works like:

TupleType<Type1,Type2, etc> removeTupleName = tupleName.removeFrom#();

So, it basically deletes the element at index #. It has no parameters, and again it returns a tuple and the elements need to match what it will have when that element is removed.

Other

In order to figure out how many elements are in a tuple use getSize(). Which is formatted as :

int nameOfVar= tupleName.getSize();

Which has no parameters and returns a int that is the number of elements in the tuple.

To find the index of something use indexOf() or lastIndexOf(). Which is formatted like:

int nameOfVar= tupleName.indexOf(Value);

int nameOfVar= tupleName.lastIndexOf(Value);

So indexof() gives you the index of the first element that matches the value. lastIndexOf() finds the last element that matches the value.

There are also methods like contains(), containsAll(), equals(), which give a boolean and check to see if a tuple has something or if a tuple equals another tuple. I might make a separate post on these or add them here at a later time.