我正在用Java制作一个地下城游戏.我创建了一个将地图存储在2D数组中的方法.该数组如下所示:
[[#, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #],
[#, ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., #],
[#, ., ., ., ., ., ., G, ., ., ., ., ., ., ., ., E, ., #],
[#, ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., #],
[#, ., ., …Run Code Online (Sandbox Code Playgroud) java printing arrays multidimensional-array indexoutofboundsexception
需要帮助找出问题readPuzzle和printPuzzle功能有什么问题.SetOfSmallInts是包含{1,2,3,4,5,6,7,8,9}这些数字的任何组合的集合.singletonSet(s)在一组中存储单个数字.无论我在程序上运行什么输入,它只输出1.即使所有输入都是' - ',输出也是81 1.有什么建议?Puzzle中的Puzzle类型构成一个数组SetofSmallInts [9][9].
//==============================================================
// readPuzzle
//==============================================================
// Reads in puzzle p from the standard input.
//==============================================================
void readPuzzle(Puzzle p)
{
int i, j;
SetOfSmallInts s;
s = rangeSet(1, 9);
char n;
for(i = 0; i < 9; i++)
{
for(j = 0; j < 9; j++)
{
n = scanf("%c", &n);
if (n == '-')
{
p[i][j]= s;
}
else if(n==1 || n==2 || n==3 …Run Code Online (Sandbox Code Playgroud) 如何使用以下结构对多维数组进行排序
arr[0] = "width:100px;height:200px;"
arr[1] = "color:#FFF;background:none;"
Run Code Online (Sandbox Code Playgroud)
我会要求输出像
arr[0] = "height:200px;width:100px;"
arr[1] = "background:none;color:#FFF;"
Run Code Online (Sandbox Code Playgroud)
它更像是在给定数组中对项目进行排序.请帮忙.
我们如何在Javascript中使用三维数组?
请出示一个基本的例子.
我尝试避免使用向量并将其替换为智能指针作为练习。目标是从智能指针中受益,从而避免内存泄漏而不依赖向量,因为这就是我现在要尝试的方法。
以下代码只是一种易于理解的方式。
我希望我的C ++代码能够正常工作。
更新:我想尽可能地使用原始c'ish风格的IO:一般来说,请不要使用std :: string或c ++流。
C版本(工作):
typedef struct{
char ** my_arr;
} MyInputs;
...
MyInputs *Doc = malloc(sizeof *Doc);
*Doc->my_arr = malloc(sizeof (*Doc->my_arr) * 2);
Doc->my_arr[0] = malloc(10);
Doc->my_arr[1] = malloc(10);
// Yes, that is stupid to alloc 10 bytes and use only 6 or 5. That is for the example.
memcpy(Doc->my_arr[0],(char*)"Hello\0",6);
memcpy(Doc->my_arr[1],(char*)"Cool\0",5);
printf("%s %s \n",Doc->my_arr[0],Doc->my_arr[1] );
...
Run Code Online (Sandbox Code Playgroud)
C ++版本(尝试):
typedef struct {
std::shared_ptr<char*>my_arr;
}MyInputs;
...
std::shared_ptr<MyInputs> MainDoc (static_cast<MyInputs*>(malloc(sizeof (*MainDoc))),free);
std::shared_ptr<char*> Z (static_cast<char**>(malloc(sizeof …Run Code Online (Sandbox Code Playgroud) 我有一个矩阵
q = [1 2 3 4 5 6;
7 8 9 10 11 12];
Run Code Online (Sandbox Code Playgroud)
我想创建一个数组d
d(:,:,1) = 1 2
7 8
d(:,:,2) = 3 4
9 10
d(:,:,3) = 5 6
11 12
Run Code Online (Sandbox Code Playgroud)
我知道如何使用循环,但我不喜欢使用循环.
我到目前为止尝试的代码:
private static List<List<List<int>>> threeDArrayToThreeDList(int [,,] letters) {
// 3d-array to 3d-list
List<List<List<int>>> letterslist = new List<List<List<int>>>();
List<List<int>> sublist = new List<List<int>> ();
List<int> subsublist = new List<int> ();
for (int i = 0; i < 2; i++) {
letterslist.Add (sublist);
for (int j = 0; j < 2; j++) {
letterslist[i].Add (subsublist);
for (int k = 0; k < 2; k++) {
Console.WriteLine (letterslist [i][j][k]); // Element not found
Console.WriteLine (letters [i,j,k]);
letterslist [i] [j] [k] = letters [i,j,k]; …Run Code Online (Sandbox Code Playgroud) 创建包含字符串和数字的数组或列表的最佳方法是什么?
使用Dictionary,可以这样做:
Dictionary < string, int > dict = new Dictionary < string, int > ();
Run Code Online (Sandbox Code Playgroud)
但是有更好的方法使用Array或List吗?
我需要它来存储IP地址和相应的端口号.
我有各种文本文件存储矩阵.我可以阅读这些内容并按照我的意愿保存它们.但是,在某些情况下,我想丢弃矩阵的列.我可以通过嵌套循环来做到这一点,但我认为应该有一种方法来实现这一点memcpy或类似的东西.这是一个例子:
#include <string.h>
using namespace std;
int main(){
int a1[4][3];
int count(0);
for (int i = 0; i < 4; ++i){
for (int j = 0; j < 3; ++j){
a1[i][j] = count;
++count;
}
}
int a2[4][2];
memcpy(a2, a1, sizeof(int) * 8);
int a3[4][2];
int *t = &a1[0][1];
memcpy(a3, t, sizeof(int) * 8);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
由于阵列被存储为行重大memcpy的结果a2和a3为整数0-7和1-8分别.我想要的是获得整数的方法
0 1
3 4
6 7
9 10
Run Code Online (Sandbox Code Playgroud)
代替.这些是前两列a1 …
如何使用for循环填充数组?我正在尝试这样做,但我需要在声明时填充我的数组.还是我错了?
我需要用二维数组来做这个,没有必要,只有2个数组也没问题