How to create 2d fixed-size array in Swift

Recently I am doing some code tests and found that fixed-size array creation is not as simple in Swift compare to other languages, i.e.

// In C++

int array1[64];    // 1-dimension array size 64
int array2[64][64] // 2-dimension array size 64x64

After a bit digging, the way to do it in swift:

// in Swift:

var array1 = [Int?](repeating: nil, count: 64) // 1 dimension array

var array2 = [[Int?]](
 repeating: [Int?](repeating: nil, count: 64)
 count: 64
) // 2-dimension array size 64x64

// Access it just like normal

array2[4][2] = 42
print(array2[4][2]) // output: 42

So a 3d array would be:

// in C++

int array3[3][3][3];

// in Swift !!!!!!

var array3 = 
[[[Int?]]](
  repeating: [[Int?]](
    repeating: [Int?](
      repeating: nil,
      count: 3),
  count: 3),
count: 3)

Now you know how, you can try 7-dimension, happy coding 🍳

[Java] Read JSON from file in Eclipse

I found myself searching for read file in java each time i need it. It think it is a good idea to write I down on my blog as a note.

Simple, import filename.json your eclipse project by drag and drop.

Call the method:

JSONObject results = readJsonFromFileWithFileName("filename.json");

The actual function

private JSONObject readJsonFromFileWithFileName(String fileName) {
JSONParser parser = new JSONParser();
JSONObject jsonObject = new JSONObject();
try {
   URL url = Test.class.getClassLoader().getResource(fileName);
   System.out.println(url.getPath());
   Object obj = parser.parse(new FileReader(url.getPath()));
   jsonObject = (JSONObject) obj;
} catch (FileNotFoundException e) {
   e.printStackTrace();
} catch (IOException e) {
   e.printStackTrace();
} catch (org.json.simple.parser.ParseException e) {

// TODO Auto-generated catch block
   e.printStackTrace();
}
   System.out.println("here" + jsonObject);
   returnjsonObject;
}