-2

I am about to generate a starting solution for a tabu search heuristic using multi-dimensional arrays. While that is fine as long as the program is up and running, as well as the computer, i would like to know, how exactly can i store the starting solution?

Let us say i got an array [10][10][10][10]. I let the program fill them with random numbers, due to a method. Now i want to "save" those values for later use. I shut down my computer for the day and would like to continue tomorrow. How would i have to do it? I don't know any code for that case. As i have to work with the chosen starting solution for several months, i cant simply let my PC run nonstop.

Any help appreciated, thanks :)

Edit: I got some code now, but i get an error: "outputFile cannot be resolved" I can not fix it so far. But i think it should work? I can not find any help on storing multidimensional arrays.

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.File;


public class speichern{

public static void main (String args[]) throws FileNotFoundException {
    PrintWriter outputfile = new PrintWriter("test.txt");
    
    int [][][]array = new int[10][10][10];
    
    int i = 0;
    int j = 0;
    int k = 0;

    int min = 50;
    int max = 100;
        
    //Generate random int value from 50 to 100 
    System.out.println("Random value in int from "+min+" to "+max+ ":");
    
    for (i=0; i < 10; i++) {
        for (j=0; j < 10; j++) {
            for (k=0; k < 10; k++) {
                //System.out.println("Test");
                array[i][j][k] = (int)Math.floor(Math.random()*(max-min+1)+min);
                outputFile.println(array[i][j][k]);
            }
        }
    }
    outputFile.close();

    for (i=0; i < 10; i++) {
        for (j=0; j < 10; j++) {
            for (k=0; k < 10; k++) {
                //System.out.println("Test");
                System.out.print(array[i][j][k]+" ");
            }
            System.out.println("");
        }
    System.out.println("i ist "+i);
    }

    System.out.println(array[8][1][3]);
}

}

  • Store to a file using some CSV format. Since it seems you're new to Java it is better to start easy, and only do more advanced stuff later. Check the answers at: https://stackoverflow.com/questions/14226830/java-csv-file-easy-read-write – Victor Polo De Gyves Montero May 10 '21 at 06:45
  • 1
    Possible duplicate of https://stackoverflow.com/q/1467193 – Robert Hume May 10 '21 at 06:51
  • You said it yourself: you have to write code that stores your data in a persistent manner. Like, saving it to a file. Thing is: don't expect us to *teach* you how to do that. There are many different ways of doing that. Any good book on Java contains chapters and chapters about this topic. You can find an endless amount of tutorials that discuss this subject, too. Do not expect us to just write such things down *again*. – GhostCat May 10 '21 at 06:56
  • Keywords to search for: "java writing text files", "java serialisation to files", ... – GhostCat May 10 '21 at 06:56

1 Answers1

0

Um, I think that there are two major ways to solve this issue. First, you can aim for saving data with local files, including Excel, CSV, text, even another sort of data saving type. Second, you can also consider saving data into a type of dataBases, such as Mysql, Oracle, and so on. But I prefer saving data with file type rather than others because It is more convenient. If you have any questions or concerns, please do not hesitate to let me know.

I look forward to hearing from you.

Best regard.

Allen.

Allen
  • 88
  • 6
  • Hey Allan. I would like to try the file attempt. I just got a 3d array now with random number from 50 to 100, which i would like to save, and import into another java projekt, with an empty array. I am searching, but not finding really helpful links on that matter. Maybe you can help me out? That would be awesome :) – user3556093 May 10 '21 at 07:24