Download nums - Brookwood High School

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
An array is a group of items all of the
same type which are accessed through
a single identifier.
int[] nums = new int[10];
nums
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
0
0
int[] nums;
nums
null
null
nothing
nums is a reference to an integer array.
new int[3];
0x213
0
0
0
arrays are Objects.
int[] nums = new int[3];
nums
0x213
0x213
0
0
0
nums is a reference to an integer array.
String s = "compsci";
s
//Strings are arrays
0
1
2
3
4
5
6
c
o
m
p
s
c
i
The first index position in a String is 0.
A String is an array of characters.
int[] nums = new int[10];
nums
//Java int array
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
0
0
Arrays are filled with 0 values when instantiated.
The exact value of each spot in the array depends
on the specified type for the array.
int[] nums = new int[10];
nums
//Java int array
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
0
0
The size must always be an int.
int[] nums = {2,7,8,234,745,1245};
0
nums
2
1
2
7
8
3
4
5
234 745 1245
An array can be initialized with values.
nums
0
1
2
3
4
5
6
7
8
9
9
0
0
0
0
0
0
0
0
0
The [spot/index] indicates which
value in the array is being manipulated.
nums[0] = 9;
The 0 spot is being set to 9.
Java indexes must always be integers
and the first index will always be 0.
nums
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
0
0
• Unlike String who uses a method to return the
length of the string…
String name = “Furman”;
int len = name.length(); //len is 6
• Arrays use a length constant. The length of an
array can not change once it is created.
int[] nums = { 1, 2, 3};
int numsLen = nums.length; // len = 3
• Notice that the method length() is followed by
parenthesis, while the constant length is not!!
int[] nums = {1,2,3,4,5,6,7};
OUTPUT
1
System.out.println(nums[0]);
2
System.out.println(nums[1]);
3
System.out.println(nums[2]);
6
System.out.println(nums[5]);
nums
0
1
2
3
4
5
6
1
2
3
4
5
6
7
int[] nums = {1,2,3,4,5,6,7};
for(int spot=0; spot<nums.length; spot++)
{
OUTPUT
System.out.println(nums[spot]);
1
}
2
length returns the # of
3
elements/items/spots in the
array!!!
4
5
6
7
int[] nums = {1,2,3,4,5,6,7};
for(int item : nums)
OUTPUT
{
1
System.out.println(item);
2
}
3
4
0 1 2 3 4 5 6
5
nums 1 2 3 4 5 6 7
6
7
//array output example 1
import static java.lang.System.*;
public class ArrayOutOne
{
public static void main(String args[])
{
int[] nums = {1,2,3,4,5,6,7};
System.out.println(nums[0]);
System.out.println(nums[1]);
System.out.println(nums[2]);
System.out.println(nums[5]);
}
}
//array output example 2
import static java.lang.System.*;
public class ArrayOutTwo
{
public static void main(String args[])
{
int[] nums = {1,2,3,4,5,6,7};
//add in a for loop to print out nums
System.out.println("\n\n");
//another bug to fix - what is the problem?
int[] intList = null;
}
}
//add in a for loop print out intList
int[] nums = new int[10];
nums[0] = 231;
nums[4] = 756;
nums[2] = 123;
OUTPUT
System.out.println(nums[0]);
System.out.println(nums[1]);
System.out.println(nums[4]);
System.out.println(nums[4/2]);
231
0
756
123
double[] nums = new double[10];
nums[0] = 10.5;
nums[3] = 98.6;
nums[2] = 77.5;
System.out.println(nums[0]);
System.out.println(nums[3]);
System.out.println(nums[7]);
OUTPUT
10.5
98.6
0.0
int[] nums = new int[6];
for(int spot=0; spot<nums.length; spot++)
{
nums[spot] = spot*4;
}
nums
0
1
2
3
4
5
0
4
8 12 16 20
//array set example 1
import static java.lang.System.*;
public class ArraySetOne
{
public static void main(String[] args)
{
int[] nums = new int[10]; //all spots set to zero to start
nums[0] = 231;
nums[4] = 756;
nums[2] = 123;
}
}
System.out.println(nums[0]);
System.out.println(nums[1]);
System.out.println(nums[4]);
System.out.println(nums[4/2]);
//array set example 2
import static java.lang.System.*;
public class ArraySetTwo
{
public static void main(String[] args)
{
int[] nums = new int[6];
for(int spot=0; spot<nums.length; spot++)
{
nums[spot] = spot*4;
}
}
}
for(int spot=0; spot<nums.length; spot++)
{
System.out.println(nums[spot]);
}
public class Array
{
private int[] nums;
//has the value null
public Array(){
nums = new int[10];
}
}
//sizes the array
//other methods not shown
public class Array
{
//instance vars and other methods not shown
public String toString()
{
String output= "";
for(int spot=0; spot<nums.length; spot++)
{
output=output+nums[spot]+" ";
}
return output;
}
}
public class Array
{
//instance vars and other methods not shown
public String toString()
{
String output= "";
for( int val : nums )
{
output = output + nums + " ";
}
return output;
}
}
String list = "7 6 3 4 9 1 3 5";
int[] nums = new int[8];
Scanner chopper = new Scanner(list);
int spot=0;
while(chopper.hasNextInt())
{
nums[spot++]=chopper.nextInt();
}
Related documents