dynamic

Calling delete on original buffer of char type after casting .c++

混江龙づ霸主 提交于 2020-12-05 12:29:26
问题 in this answer: https://stackoverflow.com/a/222578/4416169 with this code: char *buf = new char[sizeof(string)]; // pre-allocated buffer string *p = new (buf) string("hi"); // placement new string *q = new string("hi"); // ordinary heap allocation there is a comment that says: keep in mind that the strings are destrcuted manually before deleting the buffer, thats what the comment below already assumes. Strictly, it's undefined behaviour to call delete[] on the original char buffer. Using

Java initialize 2d arraylist

大憨熊 提交于 2020-11-30 14:58:21
问题 I want to do 2D dynamic ArrayList example: [1][2][3] [4][5][6] [7][8][9] and i used this code: ArrayList<ArrayList<Integer>> group = new ArrayList<ArrayList<Integer>>(); group.add(new ArrayList<Integer>(1, 2, 3)); how should i initialize this arraylist? 回答1: If it is not necessary for the inner lists to be specifically ArrayList s, one way of doing such initialization in Java 7 would be as follows: ArrayList<List<Integer>> group = new ArrayList<List<Integer>>(); group.add(Arrays.asList(1, 2,

Java initialize 2d arraylist

佐手、 提交于 2020-11-30 14:55:33
问题 I want to do 2D dynamic ArrayList example: [1][2][3] [4][5][6] [7][8][9] and i used this code: ArrayList<ArrayList<Integer>> group = new ArrayList<ArrayList<Integer>>(); group.add(new ArrayList<Integer>(1, 2, 3)); how should i initialize this arraylist? 回答1: If it is not necessary for the inner lists to be specifically ArrayList s, one way of doing such initialization in Java 7 would be as follows: ArrayList<List<Integer>> group = new ArrayList<List<Integer>>(); group.add(Arrays.asList(1, 2,