英文:
ArrayList will not add my elements of random numbers [JAVA]
问题
这是我的第一个问题,如果你了解我,你就会知道我必须完全迷失了,才会在这里提问。
我的ArrayList始终是空的,无论我做什么都无法添加元素,我只是想用1到50之间的随机数字填充它,然后将这些数字打印为字符串。我已经尝试了4-5个小时,但没有任何运气。当我运行我的代码时,没有收到错误,但ArrayList始终是空的,不会添加任何元素。如果有人能看到我做错了什么,我会非常感谢,提前谢谢。
这是我的Numbers类:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class Numbers {
private int size = 0;
ArrayList<Integer> array = new ArrayList<Integer>(size);
public Numbers() {
}
public Numbers(int n) {
size = n;
}
public void generateNumbers() {
Random random = new Random();
for (int i = 0; i < size; i++) {
array.add(random.nextInt(49) + 1);
}
}
public void findCount(int find) {
int count = Collections.frequency(array, find);
System.out.println("Number " + find + " occurred " + count + " times in the array");
}
@Override
public String toString() {
return array.toString();
}
public void isArrayCreated() {
if (size == 0) {
System.err.print("Array is not created... please create the array first");
System.out.println();
}
}
public void isEmpty() {
if (array.isEmpty()) {
System.err.print("Array is empty");
System.out.println();
System.out.println(size);
}
}
}
这是我的测试类:
import java.util.InputMismatchException;
import java.util.Scanner;
public class Lab2Test {
public static void main(String[] args) {
int n = 0;
boolean loop = true;
Scanner input = new Scanner(System.in);
while (loop == true) {
int option = 0;
System.out.println("1. Create array with new size");
System.out.println("2. Generate random numbers and store it in the array");
System.out.println("3. Search a number and display its number of occurrences");
System.out.println("4. Display array");
System.out.println("5. Quit");
System.out.println("Enter your option: ");
try {
option = input.nextInt();
} catch (InputMismatchException e) {
System.out.print("******Input mismatch exception*****");
System.out.println();
System.out.println();
}
if (option == 1) {
try {
System.out.println("Enter required size: ");
n = input.nextInt();
} catch (InputMismatchException a) {
System.out.print("******Input mismatch exception*****");
System.out.println();
System.out.println();
}
}
Numbers numb = new Numbers(n);
if (option == 2) {
numb.generateNumbers();
}
if (option == 3) {
int find = 0;
System.out.println("Enter the number to be searched: ");
try {
find = input.nextInt();
} catch (InputMismatchException e) {
System.out.print("******Input mismatch exception*****");
System.out.println();
System.out.println();
}
numb.findCount(find);
}
if (option == 4) {
numb.isEmpty();
numb.toString();
}
if (option == 5) {
System.out.println("Bye.... have a nice day!");
loop = false;
}
}
}
}
英文:
this is my first question and if you knew me you'd know id have to be completely lost to be asking a question on here.
My arraylist is continuously empty and refuses to add elements no matter what I do, I am just trying to fill it with random numbers between 1 and 50, then print those numbers as a string. I've been trying to find a solution for 4-5 hours with no luck. I'm not receiving an error when I run my code, but the arraylist is always empty and won't add any elements. If anyone can see what I'm doing wrong I would be very grateful, thank you in advance.
This is my numbers class:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class Numbers {
private int size = 0;
ArrayList<Integer> array = new ArrayList<Integer>(size);
public Numbers() {
}
public Numbers(int n) {
size = n;
}
public void generateNumbers() {
Random random = new Random();
for (int i = 0; i < size; i++) {
array.add(random.nextInt(49) + 1);
}
}
public void findCount(int find) {
int count = Collections.frequency(array, find);
System.out.println("Number " + find + " occurred " + count + " times in the array");
}
@Override
public String toString() {
return array.toString();
}
public void isArrayCreated() {
if (size == 00) {
System.err.print("Array is not created... please create the array first");
System.out.println();
}
}
public void isEmpty() {
if (array.isEmpty()) {
System.err.print("Array is empty");
System.out.println();
System.out.println(size);
}
}
}
and this is my test class:
import java.util.InputMismatchException;
import java.util.Scanner;
public class Lab2Test {
public static void main(String[] args) {
int n = 0;
boolean loop = true;
Scanner input = new Scanner(System.in);
while (loop == true) {
int option = 0;
System.out.println("1. Create array with new size");
System.out.println("2. Generate random numbers and store it in the array");
System.out.println("3. Search a number and display its number of occurrences");
System.out.println("4. Display array");
System.out.println("5. Quit");
System.out.println("Enter your option: ");
try {
option = input.nextInt();
} catch (InputMismatchException e) {
System.out.print("******Input mismatch exception*****");
System.out.println();
System.out.println();
}
if (option == 1) {
try {
System.out.println("Enter required size: ");
n = input.nextInt();
} catch (InputMismatchException a) {
System.out.print("******Input mismatch exception*****");
System.out.println();
System.out.println();
}
}
Numbers numb = new Numbers(n);
if (option == 2) {
numb.generateNumbers();
}
if (option == 3) {
int find = 0;
System.out.println("Enter the number to be searched: ");
try {
find = input.nextInt();
} catch (InputMismatchException e) {
System.out.print("******Input mismatch exception*****");
System.out.println();
System.out.println();
}
numb.findCount(find);
}
if (option == 4) {
// numb.isArrayCreated();
numb.isEmpty();
numb.toString();
}
if (option == 5) {
System.out.println("Bye.... have a nice day!");
loop = false;
}
}
}
}
答案1
得分: 1
问题出在你的Test
类中,第Numbers numb = new Numbers(n);
这一行。每次循环运行时,上一次迭代中numb
的值会被销毁,然后再次创建一个新的numb
对象。你应该将numb
变量在循环外部声明,如下所示:
import java.util.InputMismatchException;
import java.util.Scanner;
public class Lab2Test {
public static void main(String[] args) {
int n = 0;
boolean loop = true;
Numbers numb = new Numbers(0);
Scanner input = new Scanner(System.in);
while (loop == true) {
int option = 0;
System.out.println("1. Create array with new size");
System.out.println("2. Generate random numbers and store it in the array");
System.out.println("3. Search a number and display its number of occurrences");
System.out.println("4. Display array");
System.out.println("5. Quit");
System.out.println("Enter your option: ");
try {
option = input.nextInt();
} catch (InputMismatchException e) {
System.out.print("******Input mismatch exception*****");
System.out.println();
System.out.println();
}
if (option == 1) {
try {
System.out.println("Enter required size: ");
n = input.nextInt();
} catch (InputMismatchException a) {
System.out.print("******Input mismatch exception*****");
System.out.println();
System.out.println();
}
numb = new Numbers(n); // Move this line here
}
if (option == 2) {
numb.generateNumbers();
}
if (option == 3) {
int find = 0;
System.out.println("Enter the number to be searched: ");
try {
find = input.nextInt();
} catch (InputMismatchException e) {
System.out.print("******Input mismatch exception*****");
System.out.println();
System.out.println();
}
numb.findCount(find);
}
if (option == 4) {
numb.isEmpty();
numb.toString();
}
if (option == 5) {
System.out.println("Bye.... have a nice day!");
loop = false;
}
}
}
}
通常,将所有变量声明在循环外部被认为是一个好的实践方法。这也减少了你的空间复杂度,并消除了你遇到的错误的可能性。
另外,在你的函数中,你可以添加一个条件来检查if (size == 0)
。如果是这种情况,应显示适当的消息并返回适当的返回代码。这样就不会产生NullPointerException
。
英文:
The problem lies with your Test
class, in the line Numbers numb = new Numbers(n);
. Everytime your loop is run, the value of numb
from the previous iteration gets destroyed and a new object numb
is created again. You should rather declare your numb
variable outside the loop as
import java.util.InputMismatchException;
import java.util.Scanner;
public class Lab2Test {
public static void main(String[] args) {
int n = 0;
boolean loop = true;
Numbers numb = new Numbers(0);
Scanner input = new Scanner(System.in);
while (loop == true){
int option = 0;
System.out.println("1. Create array with new size");
System.out.println("2. Generate random numbers and store it in the array");
System.out.println("3. Search a number and display its number of occurrences");
System.out.println("4. Display array");
System.out.println("5. Quit");
System.out.println("Enter your option: ");
try {
option = input.nextInt();
} catch (InputMismatchException e) {
System.out.print("******Input mismatch exception*****");
System.out.println();
System.out.println();
}
if (option == 1) {
try {
System.out.println("Enter required size: ");
n = input.nextInt();
} catch (InputMismatchException a) {
System.out.print("******Input mismatch exception*****");
System.out.println();
System.out.println();
}
}
Numbers numb = new Numbers(n);
if (option == 2) {
numb.generateNumbers();
}
if (option == 3) {
int find = 0;
System.out.println("Enter the number to be searched: ");
try {
find = input.nextInt();
} catch (InputMismatchException e) {
System.out.print("******Input mismatch exception*****");
System.out.println();
System.out.println();
}
numb.findCount(find);
}
if (option == 4) {
// numb.isArrayCreated();
numb.isEmpty();
numb.toString();
}
if (option == 5) {
System.out.println("Bye.... have a nice day!");
loop = false;
}
}
}
}
Generally too, it is considered to be a nice practice to declare all your variables outside the loop. This also reduces your space complexity, and removes the possibility of such errors as encountered by you.
Also, in your functions, you can add a condition to check if (size == 0)
. If it is the case, a proper message should be displayed and an appropriate return code returned. This way no NullPointerException
will be generated.
答案2
得分: 0
问题在于Numbers对象在while循环内部被声明和实例化。因此,在每次迭代中,Numbers都会被销毁并重新创建。要解决这个问题,你应该将Numbers移到循环外部。
尝试这样做
import java.util.InputMismatchException;
import java.util.Scanner;
public class Lab2Test {
public static void main(String[] args) {
int n = 0;
boolean loop = true;
Scanner input = new Scanner(System.in);
Numbers numb; // 在这里我们在循环外部有一个对Numbers的引用,因此它不会在每次迭代中被销毁和重新创建。
while (loop == true) {
int option = 0;
System.out.println("1. 创建具有新大小的数组");
System.out.println("2. 生成随机数并将其存储在数组中");
System.out.println("3. 搜索数字并显示其出现次数");
System.out.println("4. 显示数组");
System.out.println("5. 退出");
System.out.println("输入您的选项:");
try {
option = input.nextInt();
} catch (InputMismatchException e) {
System.out.print("******输入不匹配异常*****");
System.out.println();
System.out.println();
}
if (option == 1) {
try {
System.out.println("输入所需大小:");
n = input.nextInt();
numb = new Numbers(n); // 在这里我们创建了一个新大小的Numbers
} catch (InputMismatchException a) {
System.out.print("******输入不匹配异常*****");
System.out.println();
System.out.println();
}
}
if (option == 2) {
numb.generateNumbers();
}
if (option == 3) {
int find = 0;
System.out.println("输入要搜索的数字:");
try {
find = input.nextInt();
} catch (InputMismatchException e) {
System.out.print("******输入不匹配异常*****");
System.out.println();
System.out.println();
}
numb.findCount(find);
}
if (option == 4) {
// numb.isArrayCreated();
numb.isEmpty();
numb.toString();
}
if (option == 5) {
System.out.println("再见....祝你有美好的一天!");
loop = false;
}
}
}
}
附注:在进行了这些更改之后,在选择选项1之前使用其他选项时可能会导致NullpointerExceptions,因此你需要进行修复。这只是为你的问题提供了一个解决方案,但还需要更多的工作。
英文:
The problem is that Numbers object is declared and instantiated inside the while loop.
So in each iteration your Numbers is destroyed and created again.
To solve it you should move Numbers outside the loop.
Try this
import java.util.InputMismatchException;
import java.util.Scanner;
public class Lab2Test {
public static void main(String[] args) {
int n = 0;
boolean loop = true;
Scanner input = new Scanner(System.in);
Numbers numb; // Here we have a reference to Numbers outside the loop, so it's not destroyed and recreated in each iteration.
while (loop == true) {
int option = 0;
System.out.println("1. Create array with new size");
System.out.println("2. Generate random numbers and store it in the array");
System.out.println("3. Search a number and display its number of occurrences");
System.out.println("4. Display array");
System.out.println("5. Quit");
System.out.println("Enter your option: ");
try {
option = input.nextInt();
} catch (InputMismatchException e) {
System.out.print("******Input mismatch exception*****");
System.out.println();
System.out.println();
}
if (option == 1) {
try {
System.out.println("Enter required size: ");
n = input.nextInt();
numb = new Numbers(n); // Here we create a new sized Numbers
} catch (InputMismatchException a) {
System.out.print("******Input mismatch exception*****");
System.out.println();
System.out.println();
}
}
if (option == 2) {
numb.generateNumbers();
}
if (option == 3) {
int find = 0;
System.out.println("Enter the number to be searched: ");
try {
find = input.nextInt();
} catch (InputMismatchException e) {
System.out.print("******Input mismatch exception*****");
System.out.println();
System.out.println();
}
numb.findCount(find);
}
if (option == 4) {
// numb.isArrayCreated();
numb.isEmpty();
numb.toString();
}
if (option == 5) {
System.out.println("Bye.... have a nice day!");
loop = false;
}
}
}
}
P.D.: After these changes you could get NullpointerExceptions when you use other options before 1, so you need to fix it. Just provided a solution to your problem, but it needs more work.
答案3
得分: 0
以下是您要求的翻译内容:
如@Ezequiel所说,问题在于每次您都在创建一个新对象并将其赋值给`numb`。
希望这能帮助您,现在它还会显示数组的值。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.InputMismatchException;
import java.util.Scanner;
class Numbers {
private int size;
ArrayList<Integer> array;
public Numbers() {
}
public Numbers(int n) {
size = n;
array = new ArrayList<Integer>(size);
}
public void generateNumbers() {
Random random = new Random();
for (int i = 0; i < size; i++) {
array.add(random.nextInt(49) + 1);
}
}
public void findCount(int find) {
int count = Collections.frequency(array, find);
System.out.println("Number " + find + " occurred " + count + " times in the array");
}
@Override
public String toString() {
return array.toString();
}
public void isArrayCreated() {
if (size == 0) {
System.err.print("Array is not created... please create the array first");
System.out.println();
}
}
public void isEmpty() {
if (array.isEmpty()) {
System.err.print("Array is empty");
System.out.println();
System.out.println(size);
}
}
}
public class Main {
public static void main(String[] args) {
int n = 0;
boolean loop = true;
Scanner input = new Scanner(System.in);
Numbers numb = new Numbers(n);
while (loop == true) {
int option = 0;
System.out.println("1. Create array with new size");
System.out.println("2. Generate random numbers and store it in the array");
System.out.println("3. Search a number and display its number of occurrences");
System.out.println("4. Display array");
System.out.println("5. Quit");
System.out.println("Enter your option: ");
try {
option = input.nextInt();
} catch (InputMismatchException e) {
System.out.print("******Input mismatch exception*****");
System.out.println();
System.out.println();
}
if (option == 1) {
try {
System.out.println("Enter required size: ");
n = input.nextInt();
numb = new Numbers(n);
} catch (InputMismatchException a) {
System.out.print("******Input mismatch exception*****");
System.out.println();
System.out.println();
}
}
if (option == 2) {
numb.generateNumbers();
}
if (option == 3) {
int find = 0;
System.out.println("Enter the number to be searched: ");
try {
find = input.nextInt();
} catch (InputMismatchException e) {
System.out.print("******Input mismatch exception*****");
System.out.println();
System.out.println();
}
numb.findCount(find);
numb.toString();
}
if (option == 4) {
// numb.isArrayCreated();
numb.isEmpty();
System.out.println(numb.toString());
}
if (option == 5) {
System.out.println("Bye.... have a nice day!");
loop = false;
}
}
}
}
英文:
As @Ezequiel said the problem is that every time you are creating a new object and assign it to numb
.
I hope this can help you, now it also display the value of the array
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.InputMismatchException;
import java.util.Scanner;
class Numbers {
private int size;
ArrayList<Integer> array ;
public Numbers() {
}
public Numbers(int n) {
size = n;
array = new ArrayList<Integer>(size);
}
public void generateNumbers() {
Random random = new Random();
for (int i = 0; i < size; i++) {
array.add(random.nextInt(49) + 1);
}
}
public void findCount(int find) {
int count = Collections.frequency(array, find);
System.out.println("Number " + find + " occurred " + count + " times in the array");
}
@Override
public String toString() {
return array.toString();
}
public void isArrayCreated() {
if (size == 00) {
System.err.print("Array is not created... please create the array first");
System.out.println();
}
}
public void isEmpty() {
if (array.isEmpty()) {
System.err.print("Array is empty");
System.out.println();
System.out.println(size);
}
}
}
public class Main {
public static void main(String[] args) {
int n = 0;
boolean loop = true;
Scanner input = new Scanner(System.in);
Numbers numb = new Numbers(n);
while (loop == true) {
int option = 0;
System.out.println("1. Create array with new size");
System.out.println("2. Generate random numbers and store it in the array");
System.out.println("3. Search a number and display its number of occurrences");
System.out.println("4. Display array");
System.out.println("5. Quit");
System.out.println("Enter your option: ");
try {
option = input.nextInt();
} catch (InputMismatchException e) {
System.out.print("******Input mismatch exception*****");
System.out.println();
System.out.println();
}
if (option == 1) {
try {
System.out.println("Enter required size: ");
n = input.nextInt();
numb = new Numbers(n);
} catch (InputMismatchException a) {
System.out.print("******Input mismatch exception*****");
System.out.println();
System.out.println();
}
}
if (option == 2) {
numb.generateNumbers();
}
if (option == 3) {
int find = 0;
System.out.println("Enter the number to be searched: ");
try {
find = input.nextInt();
} catch (InputMismatchException e) {
System.out.print("******Input mismatch exception*****");
System.out.println();
System.out.println();
}
numb.findCount(find);
numb.toString();
}
if (option == 4) {
// numb.isArrayCreated();
numb.isEmpty();
System.out.println( numb.toString());
}
if (option == 5) {
System.out.println("Bye.... have a nice day!");
loop = false;
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论