如何将多维数组复制到单个数组?

时间:2017-03-03 00:46:55

标签: java arrays multidimensional-array

我想将包含随机数的多维行和列的数组复制到另一个本地数组中,但只应复制行,这就是我所做的:

 arr = new int[rows][cols];
    for(int i = 0; i<arr.length; i++){
        for(int j = 0; j<arr[i].length;j++){
           arr[i][j] = (int)(range*Math.random());
        }
 public int[] getRow(int r){
    int copy[] = new int[arr.length];
    for(int i = 0; i<copy.length;i++) {
        System.arraycopy(arr[i], 0, copy[i], 0, r);
    }
    return copy;
}

4 个答案:

答案 0 :(得分:0)

2 Mar 2017 19:38:52.529 167 <190>1 2017-03-03T01:38:52.278764+00:00 app web.1 - - [2017-03-02 19:38:52 +0000] [11] [ERROR] Socket error processing request. » 2 Mar 2017 19:38:52.529 128 <190>1 2017-03-03T01:38:52.278767+00:00 app web.1 - - Traceback (most recent call last): File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 93, in handle self.handle_request(listener, req, client, addr) File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 150, in handle_request six.reraise(exc_info[0], exc_info[1], exc_info[2]) File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 134, in handle_request respiter = self.wsgi(environ, resp.start_response) File "/app/.heroku/python/lib/python2.7/site-packages/dj_static.py", line 83, in __call__ return self.application(environ, start_response) File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 189, in __call__ response = self.get_response(request) File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py", line 218, in get_response response = self.handle_uncaught_exception(request, resolver, sys.exc_info()) File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py", line 256, in handle_uncaught_exception 'request': request File "/app/.heroku/python/lib/python2.7/logging/__init__.py", line 1185, in error self._log(ERROR, msg, args, **kwargs) File "/app/.heroku/python/lib/python2.7/logging/__init__.py", line 1278, in _log self.handle(record) File "/app/.heroku/python/lib/python2.7/logging/__init__.py", line 1288, in handle self.callHandlers(record) File "/app/.heroku/python/lib/python2.7/logging/__init__.py", line 1328, in callHandlers hdlr.handle(record) File "/app/.heroku/python/lib/python2.7/logging/__init__.py", line 751, in handle self.emit(record) File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/log.py", line 129, in emit self.send_mail(subject, message, fail_silently=True, html_message=html_message) File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/log.py", line 132, in send_mail mail.mail_admins(subject, message, *args, connection=self.connection(), **kwargs) File "/app/.heroku/python/lib/python2.7/site-packages/django/core/mail/__init__.py", line 98, in mail_admins mail.send(fail_silently=fail_silently) File "/app/.heroku/python/lib/python2.7/site-packages/django/core/mail/message.py", line 303, in send return self.get_connection(fail_silently).send_messages([self]) File "/app/.heroku/python/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 100, in send_messages new_conn_created = self.open() File "/app/.heroku/python/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 58, in open self.connection = connection_class(self.host, self.port, **connection_params) File "/app/.heroku/python/lib/python2.7/smtplib.py", line 256, in __init__ (code, msg) = self.connect(host, port) File "/app/.heroku/python/lib/python2.7/smtplib.py", line 316, in connect self.sock = self._get_socket(host, port, self.timeout) File "/app/.heroku/python/lib/python2.7/smtplib.py", line 291, in _get_socket return socket.create_connection((host, port), timeout) File "/app/.heroku/python/lib/python2.7/socket.py", line 575, in create_connection raise err error: [Errno 111] Connection refused 错了。 System.arraycopy(arr[i], 0, copy[i], 0, r);是一个数组,arr[i]不是。我不知道copy[I]是什么,但不知何故我怀疑它是要复制的元素数量。查看http://docs.oracle.com/javase/8/docs/api/java/lang/System.html#arraycopy-java.lang.Object-int-java.lang.Object-int-int-处的文档,了解参数应该是什么。您需要源和目标数组具有相同的基本类型,并且两者都是数组,并且目标数组的长度足以容纳复制的元素数,这可能不是{{中的行数1}}正如您指定的那样。

答案 1 :(得分:0)

int[][] stuff = {{1,2,3}, {4,5,6}, {7,8,9}};
for (int[] thing : stuff)  println(thing);
println();
 
int[][] myClone = stuff.clone(); // Cloning the outer dimension of the 2D array.
for (int[] clone : myClone)  println(clone);
 
myClone[0][0] = 100;
print('\n', stuff[0][0]); // Prints out 100. Not a real clone
 
// In order to fix that, we must clone() each of its inner arrays too:
for (int i = 0; i != myClone.length; myClone[i] = stuff[i++].clone());
 
myClone[0][0] = 200;
println('\n', stuff[0][0]); // Still prints out previous 100 and not 200.
// It's a full clone now and not reference alias
 
exit();

答案 2 :(得分:0)

以下是使用arraycopy的正确方法:

int copy[] = new int[arr[r].length];
System.arraycopy(arr[r], 0, copy, 0, copy.length);
return copy;

编写上述内容的较短方式:

return Arrays.copyOf(arr[r], arr[r].length);

第三种方式:

return arr[r].clone();

这三种方式都会有相同的结果。至于速度,前两种方式可能比第三种方式快一点。

答案 3 :(得分:0)

我想你想要这样的东西

/**
 * Get a copy of row 'r' from the grid 'arr'.
 * Where 'arr' is a member variable of type 'int[][]'.
 *
 * @param r the index in the 'arr' 2 dimensional array
 * @return a copy of the row r
 */
private int[] getRow(int r) {
    int[] row = new int[arr[r].length];
    System.arraycopy(arr[r], 0, row, 0, row.length);
    return row;
}