Arrays: Difference between revisions

Content added Content deleted
Line 4,596: Line 4,596:


=={{header|Java}}==
=={{header|Java}}==
In Java you can create an immutable array of any ''Object'' or primitive data-type by appending the declaring type with square brackets.
<syntaxhighlight lang="java">int[] array = new int[10]; //optionally, replace "new int[10]" with a braced list of ints like "{1, 2, 3}"
<syntaxhighlight lang="java">
array[0] = 42;
String[] strings;
System.out.println(array[3]);</syntaxhighlight>
int[] values;

</syntaxhighlight>
Dynamic arrays can be made using <code>List</code>s:
Alternately, you could place the brackets after the declaring variable name, although this is discouraged as it aspects the name rather than the type.

<syntaxhighlight lang="java">
<syntaxhighlight lang="java5">List<Integer> list = new ArrayList<Integer>(); // optionally add an initial size as an argument
String strings[];
list.add(5); // appends to the end of the list
int values[];
list.add(1, 6); // inserts an element at index 1
System.out.println(list.get(0));</syntaxhighlight>
</syntaxhighlight>
Initialization can appear during the declaration, or after.
<syntaxhighlight lang="java">
String[] strings = new String[] { "rosetta", "code" };
int[] values = new int[] { 1, 2, 3 };
</syntaxhighlight>
<syntaxhighlight lang="java">
String[] strings;
strings = new String[] { "rosetta", "code" };
int[] values;
values = new int[] { 1, 2, 3 };
</syntaxhighlight>
If your arrays contents are more dynamic, and known only at runtime, you can alternately specify the array size by adding it to the assigned type's square brackets.
<syntaxhighlight lang="java">
String[] strings = new String[2];
int[] values = new int[3];
</syntaxhighlight>
To access an array element you, again, use the square-bracket syntax, specifying the element's index within it.<br />
Java indices are 0-based, so, for example, element 1 is at index 0.
<syntaxhighlight lang="java">
String string = strings[0];
int value = values[2];
</syntaxhighlight>
Here is a basic demonstration of using an array.
<syntaxhighlight lang="java">
String[] strings = new String[2];
strings[0] = "rosetta";
strings[1] = "code";
String string = strings[0] + " " + strings[1];
</syntaxhighlight>
If you printed ''string'' to the standard-out, you would get the following.
<pre>
rosetta code
</pre>
Java offers the ''Arrays'' class, which provides numerous array-related operations.<br />
A useful option is the ''Arrays.fill'' method, which can be used to initialize each element to a specified value.
<syntaxhighlight lang="java">
int[] values = new int[10];
Arrays.fill(values, 100);
</syntaxhighlight>
Additionally, you can print the contents of an array using the ''Arrays.toString'' method.
<syntaxhighlight lang="java">
Arrays.toString(values);
</syntaxhighlight>
If you printed ''values'' to the standard-out, you'd get the following.
<syntaxhighlight lang="java">
[100, 100, 100, 100, 100, 100, 100, 100, 100, 100]
</syntaxhighlight>
<br />
Java also offers a dynamic, mutable array under the Java Collections Framework ''List'' and ''Deque'' interfaces.<br />
Both which provide a substantial amount of implementing classes, for various types of dynamic array related tasks.<br />
The most logical, for this demonstration, would be the ''ArrayList'' and ''ArrayDeque''.<br /><br />
The ''ArrayList'' declaration is slightly different than that of the array.<br />
We'll use ''List'' as our declaring type, since, as with most interfaces, it's more logical to specify it as the declaring type during the instantiation of any implementing type.<br />
Immediately after the declaring type you'll use the 'diamond operators', &lt; and &gt;, with the data type of the array specified within.
<syntaxhighlight lang="java">
List<String> strings;
List<Integer> values;
</syntaxhighlight>
Similar to an array, the initialization can appear during the declaration or after.<br />
Note, the ''ArrayList'' 'diamond-operator' does not require the declared-type, as it's inferred by the declaring-type.
<syntaxhighlight lang="java">
List<String> strings = new ArrayList<>();
List<Integer> values = new ArrayList<>();
</syntaxhighlight>
Adding an element is done via the ''List.add'' method.
<syntaxhighlight lang="java">
strings.add("rosetta");
strings.add("code");
values.add(1);
values.add(2);
values.add(3);
</syntaxhighlight>
Additionally, you could specify an index at the ''List.add'' method, which will insert the element at the index, shifting the current element at that index, and all subsequent elements to the right by 1.
<syntaxhighlight lang="java">
strings.add("code");
strings.add(0, "rosetta");
</syntaxhighlight>
''List.set'' is used for mutating an already existing element.
<syntaxhighlight lang="java">
strings.set(0, "ROSETTA");
strings.set(1, "CODE");
</syntaxhighlight>
It's worth noting that Java also offers a ''Vector'' class, which is nearly similar to the ''ArrayList'' class except it should be used in multi-threaded situations, as ''ArrayList'' will produced concurrency issues.<br /><br />
The ''ArrayDeque'' is also another option for dynamic, mutable array situations, and provides a LIFO, "Last In First Out", operation for its elements.<br />
A LIFO pattern can be assimilated to a stack of plates at a buffet, as the most recent plate to be placed on the stack is the first to be taken.<br />
You declare and instantiate an ''ArrayDeque'' in the same manner as an ''ArrayList'', using the 'diamond-operators'.
<syntaxhighlight lang="java">
Deque<String> strings = new ArrayDeque<>();
</syntaxhighlight>
There are numerous methods within the ''Deque'' class for accessing and mutating the data.<br />
For this task, I'll use the most generic and logical to a LIFO pattern.<br />
To add an element you use the ''Deque.push'' method.
<syntaxhighlight lang="java">
strings.push("code");
strings.push("rosetta");
</syntaxhighlight>
To remove an item you use the ''Deque.pop'' method.<br />
Elements of a ''Deque'' are not index based.
<syntaxhighlight lang="java">
strings.pop();
</syntaxhighlight>


=={{header|JavaScript}}==
=={{header|JavaScript}}==