Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
CS 120 Introduction to OOP
Class 09 – Java arrays
Fill.java
• Write a Java program that creates several int
arrays of a specified size and fills them as
follows:
•
•
•
•
With zeros
With ones
With successive values in ascending order
With successive values in descending order
console input
import java.util.Scanner;
public class Fill {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
}
}
reading int from input
import java.util.Scanner;
public class Fill {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int size = input.nextInt();
}
}
declaring array
import java.util.Scanner;
public class Fill {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int size = input.nextInt();
int zeros[]; // reference to array
}
}
creating array
import java.util.Scanner;
public class Fill {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int size = input.nextInt();
int zeros[]; // reference to array
zeros = new int[size]; // array as object
}
}
printing array elements
import java.util.Scanner;
public class Fill {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int size = input.nextInt();
int zeros[];
zeros = new int[size];
// By default, elements initialized to 0
for (int i = 0; i < zeros.length; i++)
System.out.print(zeros[i] + " ");
System.out.println();
}
}
creating another array of same size
import java.util.Scanner;
public class Fill {
public static void main(String[] args) {
// continuation
int ones[] = new int[zeros.length];
}
}
filling array with 1
import java.util.Scanner;
public class Fill {
public static void main(String[] args) {
// continuation
int ones[] = new int[zeros.length];
for (int i = 0; i < ones.length; i++)
ones[i] = 1;
}
}
printing array
import java.util.Scanner;
public class Fill {
public static void main(String[] args) {
// continuation
int ones[] = new int[zeros.length];
for (int i = 0; i < ones.length; i++)
ones[i] = 1;
for (int i = 0; i < ones.length; i++)
System.out.print(ones[i] + " ");
System.out.println();
}
}
filling array in ascending order
import java.util.Scanner;
public class Fill {
public static void main(String[] args) {
// continuation
int straight[] = new int[zeros.length];
for (int i = 0; i < straight.length; i++)
straight[i] = i;
for (int i = 0; i < straight.length; i++)
System.out.print(straight[i] + " ");
System.out.println();
}
}
filling array in descending order
import java.util.Scanner;
public class Fill {
public static void main(String[] args) {
// continuation
int reverse[] = new int[zeros.length];
for (int i = 0; i < reverse.length; i++)
reverse[i] = reverse.length – i - 1;
for (int i = 0; i < reverse.length; i++)
System.out.print(reverse[i] + " ");
System.out.println();
}
}