0% found this document useful (0 votes)
61 views17 pages

Java Arrays: Basics and Examples

The document explains Java arrays, detailing primitive and class types, and how to declare, initialize, and access array elements. It provides examples of using arrays to calculate student exam averages and demonstrates common problems and solutions. Additionally, it covers command line arguments and how they are handled in Java applications.

Uploaded by

killaflamepyro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views17 pages

Java Arrays: Basics and Examples

The document explains Java arrays, detailing primitive and class types, and how to declare, initialize, and access array elements. It provides examples of using arrays to calculate student exam averages and demonstrates common problems and solutions. Additionally, it covers command line arguments and how they are handled in Java applications.

Uploaded by

killaflamepyro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Java

Arrays
Type
s
• Java has two kind of types
– Primitive Types
• int, byte, short, long, double, float, boolean,
char
– Class Types
• Everything else, for example
– String, Scanner, Arrays, Vector, JButton,
JCheckBox
Proble
m?
• Implement an application that will
calculate 100 students exam average.
• Variables needed?
– int studentA;
– int studentB;
– int studentC;
– int studentD;

– ...
Several Variables at Once?
•Array comes to the rescue! Just a list of variables
Declare the array
– int [] array;
•Initialize the array and set it's size
– array = new array[3];
•Store values into array
• array[0] = 2;
• array[1] = 3;
• array[2] = 7;
Array
int [] myarray = new int[3];
myarray[0] = 1;
myarray[1] = 12;
myarray[2] = 88;
[Link](myarray[0]);

[Link](myarray[1]);

[Link](myarray[2]);
inde
x

0 1 2
1 12 88
myarray

length =
3
Index
es
• The values can be retrieved from the
array using index.
• The first value is found from index 0 and
the last from length – 1
– int [] myarray = new int[LENGTH];
– myarray[0] = 1; // first one
– myarray[LENGTH-1] = 22; // last one
Initializing the Array
with {}
int [] myarray = new int[3];
myarray[0] = 2;
myarray[1] = 8;
myarray[2] = 12;

<=>

int [] myarray = {2,8,12};


Common Problems
int [] myarray1 = {2,8,12};
int [] myarray2 = new int[3];

// Output?
[Link](myarray1[3]);
// Output?
[Link](myarray2[0]));
// Output?
[Link](myarray1);
Example 1
int [] myarray = {2,8,12};
int i = 0;
while(i < 3)
{
[Link](myarray[i]);
i++;
}
Example 2
int [] myarray = {2,8,12};
int i = 0;
while(i < m y a r r a y . l
e

ng

th)
{
[Link](myarray[i]);
Example 3
int [] myarray = {2,8,12};

for(int i = 0; i < m y a r r a y . l e n g t h ; i +
+)
{
[Link](myarray[i]);
}
Example 3
int [] myarray = new int[3];

for(int i = 0; i < [Link]; i ++ )


{
myarray[i] = [Link]();
}

for(int i = [Link]-1; i >= 0; i -- )


{
[Link](myarray[i]);
}
COMMAND LINE
ARGUMENTS
Command Line
Argument?
public class CommandLine {
public static void main(S t r i n g [] a r g s
){

}
}
Declaration of an
array!
Command Line
Argument?
> java CommandLine Hello World
Hello
World

public class CommandLine


{ public static void main(St r i n g [] a r g s ) {
[Link](args[0]);
[Link](args[1]);
}
}
About Command Line
Argument
• Command line argument is user input
from the command line
• Argument array is initialized automatically
for you (size and content)

You might also like