英文:
How to extract positive numbers from one array and move them to another in Java?
问题
我可能完全做错了,但我有一个数字数组,我想从该数组中提取正数并将它们放入另一个数组中。我可能也想对负数做同样的操作。
以下是我尝试过的(代码是错误的,但我目前卡在这里):
public static void main(String[] args) {
int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};
int[] arrayPositive = Arrays.copyOf(array, 13);
for (int i = 0; i < arrayPositive.length; i++) {
if (i > 0) {
System.out.println("" + i);
}
}
}
任何反馈将不胜感激!提前感谢。
英文:
I am probably doing it all wrong but I have an array of numbers and I would like to extract positive numbers from that array and put them into a separate array. I would potentially want to do the same with negative numbers.
Here's what I've tried (the code is wrong but I am stuck here at the moment):
public static void main(String[] args) {
int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};
int [] arrayPositive = Arrays.copyOf(array, 13);
for (int i = 0; i < arrayPositive.length; i++) {
if (i > 0) {
System.out.println("" + i);
}
}
Any feedback would be more than welcome! Thanks in advance.
答案1
得分: 0
如果您想提取正数,您的条件语句应该检查每个数组元素是否大于0,就像下面的代码示例一样。
for (int i = 0; i < arrayPositive.length; ++i) {
if (arrayPositive[i] > 0) {
System.out.println("" + arrayPositive[i]);
}
}
英文:
If you want to extract positive numbers, your if should check whether each array element is greater than 0, like in the following code.
for (int i = 0; i < arrayPositive.length; ++i) {
if (arrayPositive[i] > 0) {
System.out.println("" + arrayPositive[i]);
}
}
答案2
得分: 0
public static void main(String[] args) {
int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};
int l = array.length;
int[] arrayPositive = new int[l];
int[] arrayNegative = new int[l];
int i,j,k;
i=j=k=0;
for (i = 0; i < l; i++){
if (array[i] > 0) {
arrayPositive[j]=array[i];
++j;
}
else if(array[i] < 0){
arrayNegative[k]=array[i];
++k;
}
}
for (i = 0; i < l; i++){
System.out.println(arrayPositive[i]+"\t" + arrayNegative[i]);
}
}
英文:
There you go
public static void main(String[] args) {
int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};
int l = array.length;
int[] arrayPositive = new int[l];
int[] arrayNegative = new int[l];
int i,j,k;
i=j=k=0;
for (i = 0; i < l; i++){
if (array[i] > 0) {
arrayPositive[j]=array[i];
++j;
}
else if(array[i] < 0){
arrayNegative[k]=array[i];
++k;
}
}
for (i = 0; i < l; i++){
System.out.println(arrayPositive[i]+"\t" + arrayNegative[i]);
}
}
答案3
得分: 0
public static void main(String[] args) {
int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};
int[] arrayPositive = new int[array.length];
for (int i = 0; i < array.length; i++) {
if (array[i] > 0) {
System.out.println("" + array[i]);
arrayPositive[i] = array[i];
}
}
}
英文:
public static void main(String[] args) {
int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};
int [] arrayPositive = new int[array.length];
for (int i = 0; i < array.length; i++) {
if (array[i] > 0) {
System.out.println(""+array[i] );
arrayPositive[i]=array[i];
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论