We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Java
- Data Structures
- Java 1D Array
- Discussions
Java 1D Array
Java 1D Array
+ 0 comments import java.util.*;
public class Solution {
public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int[] a = new int[n]; for (int j = 0; j < n; j++) { a[j] = scan.nextInt(); } scan.close(); // Prints each sequential element in array a for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } }
}
+ 0 comments import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; public class Solution { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int integers = sc.nextInt(); int[] myArray = new int[integers]; for (int i = 0; i < myArray.length; i++) { myArray[i] = sc.nextInt(); System.out.println(myArray[i]); } } }
+ 0 comments import java.util.*;
public class Solution {
public static void main(String[] args) { int a[] = new int[5]; Scanner scan = new Scanner(System.in); for (int j = 0; j < 5; j++) { a[j] = scan.nextInt(); } scan.close(); System.out.println("Output now"); for (int i = 0; i < a.length - 1; i++) { for (int j = 0; j < a.length - i - 1; j++) { if (a[j] > a[j + 1]) { int temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } // Prints the sorted elements in array a for (int k = 0; k < a.length; k++) { System.out.println(a[k]); } }
}
+ 0 comments The instructions mentions a loop, but there is no loop available for modification: "Modify the code in the loop so that it saves each sequential value to its corresponding location in the array. For example, the first value must be stored in , the second value must be stored in , and so on."
+ 0 comments public class Solution { public static void main(String[] args) throws IOException { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); List<Integer> arrList = new ArrayList<Integer>(); while (scan.hasNextInt()) { arrList.add(scan.nextInt()); } for(Integer i: arrList) System.out.println(i); } }
Load more conversations
Sort 255 Discussions, By:
Please Login in order to post a comment