"Here are a few examples of interesting things you can do with arrays:"
Example 1.
Fill a 10-element array with numbers from 1 to 10:
public class MainClass
{
public static void main(String[] args)
{
int[] numbers = new int[10];
for (int i = 0; i < numbers.length; i++)
{
numbers[i] = i + 1;
}
}
}
Fill a 10-element array with numbers from 10 to 1:
public class MainClass
{
public static void main(String[] args)
{
int[] numbers = new int[10];
for (int i = 0; i < numbers.length; i++)
{
numbers[i] = 10 - i;
}
}
}
Fill a 10-element array with numbers from 0 to 9:
public class MainClass
{
public static void main(String[] args)
{
int[] numbers = new int[10];
for (int i = 0; i < numbers.length; i++)
{
numbers[i] = i;
}
}
}
Fill a 10-element array with numbers from 9 to 0:
public class MainClass
{
public static void main(String[] args)
{
int[] numbers = new int[10];
for (int i = 0; i < numbers.length; i++)
{
numbers[i] = 9 - i;
}
}
}
Example 2.
Read 10 strings from the keyboard:
public class MainClass
{
public static void main(String[] args) throws IOException
{
BufferedReaderreader = new BufferedReader(new InputStreamReader(System.in));
String[] list = new String[10];
for (int i = 0; i < list.length; i++)
{
list[i] = reader.readLine();
}
}
}
Read 10 numbers from the keyboard:
public class MainClass
{
public static void main(String[] args) throws IOException
{
BufferedReaderreader = new BufferedReader(new InputStreamReader(System.in));
int[] list = new int[10];
for (int i = 0; i < list.length; i++)
{
String s = reader.readLine();
list[i] = Integer.parseInt(s);
}
}
}
Example 3.
Display an array on the screen:
public class MainClass
{
public static void main(String[] args) throws IOException
{
int[] list = new int[10];
// Fill the array
for (int i = 0; i < list.length; i++)
list[i] = i;
// Display the contents of the array
for (int i = 0; i < list.length; i++)
System.out.println(list[i]);
}
}
Example 4.
Quick (static) initialization. Add up the array elements:
public class MainClass
{
public static void main(String[] args) throws IOException
{
// Static initialization
int[] list = {5, 6, 7, 8, 1, 2, 5, -7, -9, 2, 0};
// Calculate the sum
int sum = 0;
for (int i = 0; i < list.length; i++)
sum += list[i];
System.out.println("Sum is " + sum);
}
}
Example 5.
Find the smallest element in an array:
public class MainClass
{
public static void main(String[] args) throws IOException
{
int[] list = {5, 6, 7, 8, 1, 2, 5, -7, -9, 2, 0};
int min = list[0];
for (int i = 1; i < list.length; i++)
{
if (list[i] < min)
min = list[i];
}
System.out.println ("Min is " + min);
}
}
1. Create an array of 10 strings.
2. Enter 8 strings from the keyboard and save them in the array.
3. Display the contents of the entire array (10 elements) on the screen in reverse order. Each element on a new line.
6
Task
New Java Syntax,
level 6,
lesson 3
Locked
Array of numbers in reverse order
1. Create an array of 10 numbers.
2. Enter 10 numbers from the keyboard and write them to the array.
3. Display the elements of the array in reverse order. Display each value on a new line.
Hint: what does NULL do when it appears that only 8 element slots are taken in a 10 space Array ?
Hint2: build from the examples about and then step thru the output possibilities
if you got this far is obvious that you are capable to do anything you put in your mind. Is not easy most of the times but in case to grow we need to go through discomfort. Keep it up you doing amazing job.. You are unstoppable ..
GO TO FULL VERSION