从java中未排序的数组中获取未使用的数字列表
我需要从未排序的数组中获取最小的未使用数。我无法对数组进行排序,因为这只是一个测试程序,在实际程序中,我将从一个对象数组中获取值 因此,在尝试获取最小的未使用数字的同时,我设法获得了未使用数字的列表。对于我在程序中编写的第一个随机数,它是完美的,但是第二次,输出是错误的 这是我试过的代码从java中未排序的数组中获取未使用的数字列表,java,arrays,Java,Arrays,我需要从未排序的数组中获取最小的未使用数。我无法对数组进行排序,因为这只是一个测试程序,在实际程序中,我将从一个对象数组中获取值 因此,在尝试获取最小的未使用数字的同时,我设法获得了未使用数字的列表。对于我在程序中编写的第一个随机数,它是完美的,但是第二次,输出是错误的 这是我试过的代码 class smallestUnusedNumberTest { public static void main(String args[]) { int[] testArray = {
class smallestUnusedNumberTest {
public static void main(String args[]) {
int[] testArray = {1, 5, 7, 11, 4, 8};
int largest = 1;
int i;
for(i = 0; i < testArray.length; i++) {
if(testArray[i] > largest) {
largest = testArray[i];
}
}
for(i = 1; i < largest; i++) {
for(int j = 0; j < testArray.length; j++) {
if(i == testArray[j]) {
i++;
}
}
System.out.println(i);
}
}
}
我得到5,它已经在数组中了。
我使用了一个for
循环从数组中获取最大的数字。但是,我无法找出正确的逻辑
如何从数组中获取正确的未使用数字?我需要按升序输出。算法的逻辑有问题:当找到匹配项时,在内部循环中递增
I
,但继续循环。因此,当您在5
之后找到4
时,您将i
从4
增加到5
,但您永远不会返回到数组的开头以查看之前的一些元素是否为5
要解决此问题,请在外循环中定义一个布尔
变量,最初将其设置为false
,然后在找到i==testArray[j]
时,在内循环中将其设置为true
;当你找到一个匹配的时候,打破循环
检查内部循环后的布尔变量。如果它是
true
,则数字在那里,因此您不应该打印任何内容。否则,请打印数字。使用标志以了解该值是否未使用
public static void main(String[] args) {
int[] testArray = {1, 5, 7, 11, 4, 8};
int largest = 1;
int i;
for(i = 0; i < testArray.length; i++) {
if(testArray[i] > largest) {
largest = testArray[i];
}
}
for(i = 1; i < largest; i++) {
boolean unused = true;
for(int j = 0; j < testArray.length; j++) {
if(i == testArray[j]) {
unused = false;
break;
}
}
if (unused)
System.out.println(i);
}
}
publicstaticvoidmain(字符串[]args){
int[]testArray={1,5,7,11,4,8};
int最大=1;
int i;
对于(i=0;i最大值){
最大=测试阵列[i];
}
}
对于(i=1;i<最大值;i++){
布尔值=真;
对于(int j=0;j
结果:
如果您创建一个子方法,这将使您的生活更加轻松
//returns true if the array parameter contains num, false if not
private boolean contains(int num, int[] array) {
// fill this in
}
希望这有助于
package com.test;
import java.util.Arrays;
public class SmallestUnused {
public static void main(String[] args) {
int[] testArray = {1, 5, 7, 11, 4, 8};
Arrays.sort(testArray);
int smallest = testArray[0];
int largest = testArray[testArray.length-1];
int smallestUnused = largest + 1;
System.out.println("smallest: "+smallest);
System.out.println("largest: "+largest);
if(smallest>1){
smallestUnused = 1;
}else{
for(int i=2; i<largest; i++){
if(Arrays.binarySearch(testArray, i)<0){
smallestUnused = i;
break;
}
}
}
System.out.println("Smallest unused: "+smallestUnused);
}
}
package.com.test;
导入java.util.array;
公共类小型计算机{
公共静态void main(字符串[]args){
int[]testArray={1,5,7,11,4,8};
数组。排序(testArray);
int最小=测试阵列[0];
int最大=测试阵列[testArray.length-1];
int smallestUnused=最大值+1;
System.out.println(“最小:”+最小);
System.out.println(“最大:”+最大);
如果(最小值>1){
最小未使用=1;
}否则{
对于(inti=2;i您可以通过如下方式简化代码来减少循环-
public static void main(String[] args) {
int[] testArray = {1, 5, 7, 11, 4, 8};
int i = 0;
while (true) {
i++;
boolean small = false;
boolean unused = true;
for (int j : testArray) {
if (i == j) {
unused = false;
break;
} else if (i < j) {
small = true;
}
}
if (small && unused) {
System.out.println(i);
} else if (!small && unused) {
break;
}
}
}
publicstaticvoidmain(字符串[]args){
int[]testArray={1,5,7,11,4,8};
int i=0;
while(true){
i++;
布尔小=假;
布尔值=真;
用于(int j:testArray){
如果(i==j){
未使用=错误;
打破
}else if(i
这可以通过四个步骤解决,无需任何嵌套循环
在testArray中查找最大值。您已经有了相应的代码
创建一个新的布尔数组,最大值+1,所有值初始化为false。我们将其称为usedValues[]
再次循环testArray。对于testArray的每个元素,将UsedValue[testArray[i]]=true
最后,循环usedValues[]。usedValues[i]==false的第一个元素的索引是testArray中的最低未使用值
如果您说要按升序打印所有未使用的数字,只需通过usedValues数组一次即可,只需打印usedValues[i]所在的所有元素的索引==false。为什么不让对象实现可比较的接口并对它们进行排序?是的……这就是我所缺少的。我在I++下面添加了j=0,现在它工作得很好。这几乎可以肯定是一个学习练习。为OP编写代码并不能帮助他或她学习,即使你的代码100%正确。我假设我们是相互关联的只在自然数上受尊重。
public static void main(String[] args) {
int[] testArray = {1, 5, 7, 11, 4, 8};
int i = 0;
while (true) {
i++;
boolean small = false;
boolean unused = true;
for (int j : testArray) {
if (i == j) {
unused = false;
break;
} else if (i < j) {
small = true;
}
}
if (small && unused) {
System.out.println(i);
} else if (!small && unused) {
break;
}
}
}