[英]How to print [ ] in an array (Java)
I want to take 10 numbers from the user and input them into a new array. 我想从用户那里获取10个数字并将其输入到新数组中。 How can I put [ ] into my output?
如何将[]放入输出中?
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int num = 10;
List<Integer> inputList = new ArrayList<>(num);
while (num-- > 0) {
inputList.add(scnr.nextInt());
}
List<Integer> goofyArray = new ArrayList<>(inputList.size());
for (int i = inputList.size() - 1; i >= 0; i--) {
if(inputList.get(i) % 7 == 0){continue;}
if(inputList.get(i) < 0){
goofyArray.add((inputList.get(i) * -1) * 2);
} else {
goofyArray.add(inputList.get(i));
}
}
for (int number : goofyArray) {
System.out.print( number + " ");
}
}
}
Not sure about what do you mean with put [ ] into my output .Try this: 不确定将[]放到我的输出中是什么意思。试试看:
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int num = 10;
List<Integer> inputList = new ArrayList<>(num);
while (num-- > 0) {
inputList.add(scnr.nextInt());
}
List<Integer> goofyArray = new ArrayList<>(inputList.size());
//Reverse the array.
for (int i = inputList.size() - 1; i >= 0; i--) {
if(inputList.get(i) % 7 == 0){continue;} //doesn't add numbers that are multiples of 7.
if(inputList.get(i) < 0){
goofyArray.add((inputList.get(i) * -1) * 2); // Change any numbers that are negative to be positive and twice their value.
} else {
goofyArray.add(inputList.get(i));
}
}
int[] output = new int[goofyArray.size()];
output = goofyArray.toArray(output);
for (int number : output) {
System.out.print( number + " ");
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int NUM_OF_INPUTS = 10;
int [ ] inputArray = new int [NUM_OF_INPUTS];
for (int index = 0; index < NUM_OF_INPUTS; index++){
inputArray[index] = scnr.nextInt();
}
int[] goofyArray = new int[inputArray.length];
int[] arrayWithoutSevensAndReversed; // make a new array because static arrays don't have a remove
int multiplesOfSeven = 0;
// for loop 1: reverse and check how many multiples of 7
for (int i = 0; i < goofyArray.length; i++) {
goofyArray[i] = inputArray[goofyArray.length - 1 - i]; // Reverse the array.
if (goofyArray[i] % 7 == 0)
multiplesOfSeven++;
}
// for loop 2: use the new array with that will not contains multiples of seven the array
arrayWithoutSevensAndReversed = new int[goofyArray.length - multiplesOfSeven];
for (int x = 0, y = 0; x < goofyArray.length; x++) {
if (goofyArray[x] % 7 != 0) { // is not multiple of 7, then add it to the array!
arrayWithoutSevensAndReversed[y] = goofyArray[x];
if (arrayWithoutSevensAndReversed[y] < 0) { // make it positive then double!
arrayWithoutSevensAndReversed[y] = arrayWithoutSevensAndReversed[y] * -1 * 2;
}
y++;
}
}
for (int number : arrayWithoutSevensAndReversed) {
System.out.print(number + " ");
}
}
}
How can I put [ ] into my output?
如何将[]放入输出中?
Use Arrays.toString(int array[]) from util package. 使用util包中的Arrays.toString(int array []) 。
System.out.println(Arrays.toString(goofyArray));
As far as I understand you want to print the array as output. 据我了解,您想将数组打印为输出。
All you have to do is instead of this: 您要做的就是代替此:
for (int number : goofyArray) {
System.out.print( number + " ");
}
do this: 做这个:
System.out.print(goofyArray.toString());
Instead of terming it as delete, you can rather shift the i+1 th term to the ith term 与其将其称为删除,不如将其移至第i + 1个词
if ( goofyArray[i] % 7 == 0){
for(int x=i;x<goofyArray.length-1;x++){
goofyArray[x]=goofyArray[x+1];
}
Here the ith term gets deleted. 在这里,第i项被删除。
To remove elements from array, you can use ArrayUtils.remove(int array[], int index) from org.apache.commons.lang package. 要从数组中删除元素,可以使用org.apache.commons.lang包中的ArrayUtils.remove(int array [],int index) 。
int num[]=new int[] {10,20,30,40,50};
num = ArrayUtils.remove(num, 2);
If there are any 42s in the array, I want my code to remove all other numbers and simply make goofyArray contain 1 element 42. In this example we have two 42s and remaining are others numbers.
如果数组中有任何42,我希望我的代码删除所有其他数字,并使goofyArray包含1个元素42。在此示例中,我们有两个42,其余为其他数字。 Hence it will remove all other numbers except 42.
因此,它将删除除42之外的所有其他数字。
public static void main(String[] args) {
int[] inputArray = { 5, -2, 42, 45, -6, 8, 42, -9, 10, 7 };
int[] tempArray = new int[inputArray.length];
int count=0;
for (int i = 0; i < inputArray.length; i++) {
if(inputArray[i]==42) {
tempArray[count]=42;
count++;
}
}
int[] goofArray=Arrays.copyOf(tempArray, count);
for(int i=0;i<goofArray.length;i++)
System.out.print(goofArray[i]+" ");
}
Before editing your question was: You wanted to reverse an array.
在编辑问题之前,您需要:反转数组。 If the array contains any negative number then first make it positive then twice(-9 will become 18) it.
如果数组包含任何负数,则先使其为正数,然后使其两次(-9将变为18)。 If the array contains an element which is divisble by 7 then remove it.
如果数组包含一个可被7整除的元素,则将其删除。 This example will fullfil your requirement.
本示例将满足您的要求。
public static void main(String[] args) {
int[] inputArray = { 5, -2, 28, 45, -6, 8, 42, -9, 10, 7 };
int length = inputArray.length;
int[] goofyArray = new int[length];
int count=0;
for (int i = 0; i < goofyArray.length; i++) {
if (inputArray[length - 1 - i] % 7 != 0) {
goofyArray[count] = inputArray[length - 1 - i]; // Reverse the array.
if (goofyArray[count] < 0) {
goofyArray[count] = Math.abs(goofyArray[count]) * 2;
}
count++;
}
}
int[] goofArray2=Arrays.copyOf(goofyArray, count);
for(int i=0;i<goofArray2.length;i++)
System.out.print(goofArray2[i]+" ");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.