Files
Examples/Java/Processing/processing-3.3.6/modes/java/reference/Array.html
2018-04-20 10:15:15 +01:00

220 lines
9.1 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Array \ Language (API) \ Processing 2+</title>
<link rel="icon" href="/favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Author" content="Processing Foundation" />
<meta name="Publisher" content="Processing Foundation" />
<meta name="Keywords" content="Processing, Sketchbook, Programming, Coding, Code, Art, Design" />
<meta name="Description" content="Processing is a flexible software sketchbook and a language for learning how to code within the context of the visual arts. Since 2001, Processing has promoted software literacy within the visual arts and visual literacy within technology." />
<meta name="Copyright" content="All contents copyright the Processing Foundation, Ben Fry, Casey Reas, and the MIT Media Laboratory" />
<script src="javascript/modernizr-2.6.2.touch.js" type="text/javascript"></script>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body id="Langauge-en" onload="" >
<!-- ==================================== PAGE ============================ -->
<div id="container">
<!-- ==================================== HEADER ============================ -->
<div id="ribbon">
<ul class="left">
<li class="highlight"><a href="http://processing.org/">Processing</a></li>
<li><a href="http://p5js.org/">p5.js</a></li>
<li><a href="http://py.processing.org/">Processing.py</a></li>
<li><a href="http://android.processing.org/">Processing for Android</a></li>
</ul>
<ul class="right">
<li><a href="https://processingfoundation.org/">Processing Foundation</a></li>
</ul>
<div class="clear"></div>
</div>
<div id="header">
<a href="/" title="Back to the Processing cover."><div class="processing-logo no-cover" alt="Processing cover"></div></a>
<form name="search" method="get" action="//www.google.com/search">
<p><input type="hidden" name="as_sitesearch" value="processing.org" />
<input type="text" name="as_q" value="" size="20" class="text" />
<input type="submit" value=" " /></p>
</form>
</div>
<a id="TOP" name="TOP"></a>
<div id="navigation">
<div class="navBar" id="mainnav">
<a href="index.html" class='active'>Language</a><br>
<a href="libraries/index.html" >Libraries</a><br>
<a href="tools/index.html">Tools</a><br>
<a href="environment/index.html">Environment</a><br>
</div>
<script> document.querySelectorAll(".processing-logo")[0].className = "processing-logo"; </script>
</div>
<!-- ==================================== CONTENT - Headers ============================ -->
<div class="content">
<p class="ref-notice">This reference is for Processing 3.0+. If you have a previous version, use the reference included with your software in the Help menu. If you see any errors or have suggestions, <a href="https://github.com/processing/processing-docs/issues?state=open">please let us know</a>. If you prefer a more technical reference, visit the <a href="http://processing.github.io/processing-javadocs/core/">Processing Core Javadoc</a> and <a href="http://processing.github.io/processing-javadocs/libraries/">Libraries Javadoc</a>.</p>
<table cellpadding="0" cellspacing="0" border="0" class="ref-item">
<tr class="name-row">
<th scope="row">Name</th>
<td><h3>Array</h3></td>
</tr>
<tr class="">
<tr class=""><th scope="row">Examples</th><td><div class="example"><pre >
int[] numbers = new int[3];
numbers[0] = 90; // Assign value to first element in the array
numbers[1] = 150; // Assign value to second element in the array
numbers[2] = 30; // Assign value to third element in the array
int a = numbers[0] + numbers[1]; // Sets variable 'a' to 240
int b = numbers[1] + numbers[2]; // Sets variable 'b' to 180
</pre></div>
<hr class="noshade" noshade="noshade" size="1">
<div class="example"><pre >
int[] numbers = { 90, 150, 30 }; // Alternate syntax
int a = numbers[0] + numbers[1]; // Sets variable 'a' to 240
int b = numbers[1] + numbers[2]; // Sets variable 'b' to 180
</pre></div>
<hr class="noshade" noshade="noshade" size="1">
<div class="example"><pre >
int degrees = 360;
float[] cos_vals = new float[degrees];
// Use a for() loop to quickly iterate
// through all values in an array.
for (int i=0; i < degrees; i++) {
cos_vals[i] = cos(TWO_PI/degrees * i);
}
</pre></div>
<hr class="noshade" noshade="noshade" size="1">
<div class="example"><pre >
float[] randoms = new float[100];
for (int i = 0; i < randoms.length; i++) {
randoms[i] = random(100);
}
// You can also use an enhanced loop
// to access the elements of an array
for (float val : randoms) {
println(val);
}
// This works with arrays of objects, too,
// but not when first making the array
PVector[] vectors = new PVector[5];
for (int i = 0; i < vectors.length; i++) {
vectors[i] = new PVector();
}
// The syntax only applies when iterating
// over an existing array
for (PVector v : vectors) {
point(v.x, v.y);
}
</pre></div>
</td></tr>
<tr class="">
<th scope="row">Description</th>
<td>
An array is a list of data. It is possible to have an array of any type of data. Each piece of data in an array is identified by an index number representing its position in the array. The first element in the array is <b>[0]</b>, the second element is <b>[1]</b>, and so on. Arrays are similar to objects, so they must be created with the keyword <b>new</b>.<br/>
<br/>
Each array has a variable <b>length</b>, which is an integer value for the total number of elements in the array. Note that since index numbering begins at zero (not 1), the last value in an array with a <b>length</b> of 5 should be referenced as <b>array[4]</b> (that is, the <b>length</b> minus 1), not <b>array[5]</b>, which would trigger an error.<br/>
<br/>
Another common source of confusion is the difference between using <b>length</b> to get the size of an array and <b>length()</b> to get the size of a String. Notice the presence of parentheses when working with Strings. (<b>array.length</b> is a variable, while <b>String.length()</b> is a method specific to the String class.)
</td>
</tr>
<tr class=""><th scope="row">Syntax</th><td><pre>
datatype[] var
var[element] = value
var.length
</pre></td></tr>
<tr class=""> <th scope="row">Parameters</th><td><table cellpadding="0" cellspacing="0" border="0"><tr class="">
<th scope="row" class="code">datatype</th>
<td>any primitive or compound datatype, including user-defined classes</td>
</tr>
<tr class="">
<th scope="row" class="code">var</th>
<td>any valid variable name</td>
</tr>
<tr class="">
<th scope="row" class="code">element</th>
<td>int: must not exceed the length of the array minus 1</td>
</tr>
<tr class="">
<th scope="row" class="code">value</th>
<td>data to assign to the array element; must be the same datatype as the array</td>
</tr></table></td> </tr>
</table>
Updated on April 30, 2017 02:33:22pm EDT<br /><br />
<!-- Creative Commons License -->
<div class="license">
<a rel="license" href="//creativecommons.org/licenses/by-nc-sa/4.0/"><img alt="Creative Commons License" style="border: none" src="https://licensebuttons.net/l/by-nc-sa/4.0/88x31.png" /></a>
</div>
<!--
<?xpacket begin='' id=''?>
<x:xmpmeta xmlns:x='adobe:ns:meta/'>
<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>
<rdf:Description rdf:about=''
xmlns:xapRights='http://ns.adobe.com/xap/1.0/rights/'>
<xapRights:Marked>True</xapRights:Marked>
</rdf:Description>
<rdf:Description rdf:about=''
xmlns:xapRights='http://ns.adobe.com/xap/1.0/rights/'
>
<xapRights:UsageTerms>
<rdf:Alt>
<rdf:li xml:lang='x-default' >This work is licensed under a &lt;a rel=&#34;license&#34; href=&#34;http://creativecommons.org/licenses/by-nc-sa/4.0/&#34;&gt;Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License&lt;/a&gt;.</rdf:li>
<rdf:li xml:lang='en' >This work is licensed under a &lt;a rel=&#34;license&#34; href=&#34;http://creativecommons.org/licenses/by-nc-sa/4.0/&#34;&gt;Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License&lt;/a&gt;.</rdf:li>
</rdf:Alt>
</xapRights:UsageTerms>
</rdf:Description>
<rdf:Description rdf:about=''
xmlns:cc='http://creativecommons.org/ns#'>
<cc:license rdf:resource='http://creativecommons.org/licenses/by-nc-sa/4.0/'/>
</rdf:Description>
</rdf:RDF>
</x:xmpmeta>
<?xpacket end='r'?>
-->
</div>
<!-- ==================================== FOOTER ============================ -->
<div id="footer">
<div id="copyright">Processing is an open project intiated by <a href="http://benfry.com/">Ben Fry</a> and <a href="http://reas.com">Casey Reas</a>. It is developed by a <a href="http://processing.org/about/people/">team of volunteers</a>.</div>
<div id="colophon">
<a href="copyright.html">&copy; Info</a>
</div>
</div>
</div>
<script src="javascript/jquery-1.9.1.min.js"></script>
<script src="javascript/site.js" type="text/javascript"></script>
</body>
</html>