Так что я в основном создаю сумку, и мне сказали, что один из моих сумок должен содержать не менее 100000 элементов. Это не будет входить в консольное окно eclipse. Поэтому запись информации в файл кажется лучшей идеей. Я применил мой основной метод и метод runBigBad. Как вы можете видеть, после runBigBag больше я хочу сделать. Он компилируется и работает нормально. Тем не менее, он печатает все после вызова runBigBag() в текстовый файл. Мне просто нужно содержимое runBigBag() в текстовом файле, а остальное - для печати в окне консоли. Кто-нибудь может мне помочь? Когда я пытаюсь использовать предложение finally и выполняю ps.close(), он хочет, чтобы я инициализировал ps, который затем делает файл нулевым. Поэтому я не уверен, что делать, если нет более простого способа.Закрытие printstream
public class TestBag {
public static <E> void main(String[] args) {
//Start time for WHOLE PROGRAM
final long start = System.currentTimeMillis();
//Run small bag
runSmallBag();
//RESET COUNTERS TO ALLOW BIG BAG TO KEEP TRACK OF COMPLEXITY
Bag.resetCounters();
//Run big bag
runBigBag();
//Display the operation counters for each method used in whole program
System.out.println("Number of times add() was used for whole program: " + Bag.addCountA + "\n");
System.out.println("Number of times remRand() was used for whole program: " + Bag.ranRemCountA + "\n");
System.out.println("Number of times rem() was used for whole program: " + Bag.remCountA + "\n");
System.out.println("Number of times contains() was used whole program: " + Bag.containsCountA + "\n");
System.out.println("number of times addAll() was used whole program: " + Bag.addAllCountA + "\n");
System.out.println("Number of times union() was used whole program: " + Bag.unionCountA + "\n");
System.out.println("Number of times equal() was used whole program: " + Bag.equalsCountA + "\n");
//End timer for whole program
final long end = System.currentTimeMillis();
//Display timer for whole program
System.out.println("The whole program took " + ((end - start)*.001) + " seconds to run");
}
public static <E> void runBigBag(){
//Start time for big bag operations
final long start2 = System.currentTimeMillis();
boolean prog = true;
//Create file to write bag too
File file = new File("bag.txt");
PrintStream ps;
try {
ps = new PrintStream(new FileOutputStream(file));
System.setOut(ps);
} catch (FileNotFoundException f) {
f.printStackTrace();
} finally
//irrelevant code here
//End timer for big bag
final long end2 = System.currentTimeMillis();
//Display timer for big bag
System.out.println("This part of the program took " + ((end2 - start2)*.001) + " seconds to run");
}
}