Я пытаюсь распечатать все индексы логического массива, где его элемент равен true.Конечная цель - найти простое число индексов (где я заменяю каждое индексное число, не являющееся простым на ложное в массиве), а затем распечатать только то, что осталось от простых чисел индексов массива..
Самый первый шаг, который я просто пытаюсь сделать, - это, по крайней мере, получить некоторый целочисленный индекс для распечатки, но, похоже, ничего не работает, и я не знаю, чтонеправильно.
public class PriNum{
private boolean[] array;
public PriNum(int max){
if (max > 2){ //I don't have any problems with this if statement
throw new IllegalArgumentException();
}
else{
array = new boolean[max];
for(int i = 0; i < max; i++){
if(i == 0 || i == 1){ //Automatically makes 0 and 1 false
//because they are not prime
array[i] = false;
}
else{
array[i] = true;
}
}
toString(); //I know for sure the code gets to here
//because it prints out a string I have
// there, but not the index
}
}
public String toString(){
String s = "test"; //this only prints test so I can see if
//the code gets here, otherwise it would just be ""
for (int i = 0; i < array.length; i++){
if(array[i] == true){
s = s + i; //Initially I tried to have the indexes returned
//to be printed and separated by a comma,
//but nothing comes out at all, save for "test"
}
}
return s;
}
}
РЕДАКТИРОВАТЬ: Включен класс драйвера, который запрашивает печать класса PriNum
class Driver{
public static void main(String [] args){
PriNum theprime = null;
try{
theprime = new PriNum(50);
}
catch (IllegalArgumentException oops){
System.out.println("Max must be at least 2.");
}
System.out.println(theprime);
}
}