java一条for循环打印多维数组

java实现 逆时针打印二维数组 立即下载
N维规则矩阵数组,可以只用一条语句遍历。
public class Main {
    public static void main(String[] args) {
        System.out.println("----------二维数组----------");
        int arr[][] = {
            {1,1,1},{2,2,2},{3,3,3}
        };
        int arr_row = arr.length;
        int arr_col = arr[0].length;
        for (int i = 0; i < arr_row * arr_col; i++)
            System.out.print(arr[i / arr_col][i % arr_row] + " ");


        System.out.println("\n\n----------三维数组----------");
        int brr[][][] = {
                    {
                {1,2,3},{4,5,6}
            },{
                {7,8,9},{10,11,12}
            }
        };
        int brr_row = brr.length;
        int brr_col = brr[0].length;
        int brr_height = brr[0][0].length;

        for (int i = 0; i < brr_row * brr_col * brr_height; i++)
            System.out.print(brr[i / brr_col / brr_height][i / brr_height % brr_col][i % brr_height] + " ");


        System.out.println("\n\n----------四维数组----------");
        int crr[][][][] = {
                        {
                      {
                    {1,2,3},{4,5,6} 
                },{
                    {7,8,9},{10,11,12}
                },{
                    {13,14,15},{16,17,18}
                }
            }
        };
        int crr_row = crr.length;
        int crr_col = crr[0].length;
        int crr_height = crr[0][0].length;
        int crr_width = crr[0][0][0].length;
        for (int i = 0; i < crr_row * crr_col * crr_height * crr_width; i++)
            System.out.print(crr[i/crr_col/crr_height/crr_width][i/crr_width/crr_height%crr_col][i/crr_width%crr_height][i%crr_width] + " ");
    }
}
运行结果:
----------二维数组----------
1 1 1 2 2 2 3 3 3 

----------三维数组----------
1 2 3 4 5 6 7 8 9 10 11 12 

----------四维数组----------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 
对于此法,开阔思维可以,项目中使用不但无利于代码维护,且实际效率略低于多重for循环遍历。

欢迎给出不同实现及深层次探讨。
输出一个矩阵java java表示矩阵,然后可以表示任意的N阶矩阵 立即下载

相关推荐

Ubuntu下为RK3568开发板搭建Qt交叉编译环境:手把手教你配置Qt Creator和远程调试

本文详细介绍了在Ubuntu系统下为RK3568开发板搭建Qt交叉编译环境的完整流程,包括工具链配置、Qt源码交叉编译、Qt Creator环境设置以及远程调试技巧。通过手把手教程,开发者可以快速掌握RK3568平台的Qt开发环境搭建方法,实现高效的应用开发和部署。

weixin_30776273的博客 372

JAVA——二维数组打印

主题:二维数组打印 二维数组,以2行3列的数组array[2][3]为例。array数组的长度为行的长度2,每个行元素又是一个一维数组。列数可以为空,行数不可以为空。array[2][]会产生控制指针异常。 方法一:for循环 代码: public static void main(String[] args) { int[][]array={{1,2,3},{4,5,6}}; for(int row=0;row<array.length;row++){

SwitchD的博客 8075

RAG-Anything 实战:构建企业级多模态知识图谱问答系统

本文详细介绍了如何利用RAG-Anything构建企业级多模态知识图谱问答系统,解决跨模态文档处理的痛点。通过架构设计解析、部署实战指南和典型业务场景解决方案,帮助企业实现高效的知识管理和智能检索,提升决策效率。

weixin_29089057的博客 385

用for循环和增强for循环遍历多维数组

/** * 遍历多维数组 * @author Administrator * */ public class Test2 { public static void main(String[] args) { int[][] arr = new int[][]{{10,20,30},{100,200,300}}; //int[] arr0

来看日出的博客 8616

YOLOv8交通管理 详细解释及代码完整示例

YOLOv8在交通管理中的应用及代码实现 摘要:本文详细介绍了YOLOv8在交通管理领域的应用场景,包括车辆检测、违规行为识别和行人安全预警等。文章提供了完整的Python代码示例,展示了如何使用YOLOv8进行交通对象检测和车辆计数统计。代码包含模型加载、对象检测、结果可视化等功能模块,并给出了性能优化和功能扩展建议。该实现基于Ultralytics框架,支持多种YOLOv8模型选择,可根据实际需求调整模型大小和检测参数。

u010117029的博客 152

OpenClaw 安装失败:npm install 多次报错的完整排错与解决方案

这次问题的最终解决,其实并不复杂,但关键在于思路的转变:从“脚本怎么又失败了”,切换为“npm 在这一层到底失败在哪里”。当我们手动复现安装过程、打开 verbose 日志、直面真实错误栈时,问题就不再是模糊的“安装不了”,而是一个可以被定位、被验证、被修复的具体工程问题。

博客 1万+

【Java SE】数组的打印方式

java中打印数组常用的几种方法: 在Java开发中有时候需要打印数组信息,下面是整理归纳的几种打印数组方法:(如下) 一维数组用 Arrays.toString() 方法,多维数组用 Arrays.deepToString() 方法

书生-w的博客 1万+

Plecs电力电子仿真专业教程-第一季 第一节 Plecs简介

Plces 电力电子仿真专业教程系列依托专业的电力电子仿真软件Plecs对各种电力电子常用的拓扑及实际设计的方案进行系统及仿真与软件代码仿真实践。通过对各种电路仿真原理的讲解及专业C语言代码程序的编写,提升学员的系统设计能力,软件编程能力与理论分析能力。

3637

java中遍历打印二维数组

在java中,打印二维数组跟一维数组类似,也是使用for循环遍历 不同的是,在二维里面用 j < a [ i ].length;j++ 可以理解为,在第一层数组内的每个a[ i ]都是一个普通数组 外层输出了{a[ 1 ]、a[ 2] 、a[ 3] } 内层输出每一个a[ i ]里面的元素 class abb { public static void main(String args[]) { int[][] a = {{1, 2, 3}, {4, 5, 6

weixin_62128054的博客 1694

图文教程本地部署开源扣子Coze Studio工作流并接入 Gemini 大模型(windows亲测) Coze Studio 本地部署、Gemini 模型接入、开源 AI 工具平台、大模型工作流搭建

本文介绍了在Windows系统本地部署开源AI工具平台Coze Studio并接入Google Gemini大模型的完整教程。主要内容包括:1) 环境准备要求(Docker等);2) 获取源码和配置Gemini模型(需API密钥);3) 通过Docker Compose部署服务;4) 浏览器访问验证。文章指出该平台适合私有化部署,支持智能体和工作流搭建,但也存在插件系统不完善等局限。最后提供了相关资源链接,为开发者实现本地AI解决方案提供了实用指南。

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 2779

你学Simulink--基于储能系统的建模与仿真场景:飞轮储能系统在电网调频中的作用仿真

本文详细介绍了利用Simulink+Simscape搭建飞轮储能系统(FESS)电网调频仿真模型的全过程。主要内容包括:系统工作原理及调频作用分析,基于Simulink的模块化建模步骤(飞轮、电机/发电机、功率电子接口等组件搭建),调频控制策略实现(频率检测、功率指令生成和模式切换),以及仿真参数设置与结果分析方法。该模型可有效模拟FESS在电网频率调节中的快速响应特性,为新能源存储和智能电网研究提供重要仿真工具,同时可通过参数优化和算法改进进一步提升系统性能。

xiaoheshang_123的博客 168

java打印出二维数组方法

java打印出二维数组的方法 首先:定义一个date数组for循环这个数组 for (int[] date : my_arr) 1. 第一种方法: 直接用Arrays.toString(date)方法 System.out.println(Arrays.toString(date)); 总代码如下: package 基础; import java.util.Arrays; public class Demo { public static void main(String[] args) {

maoxu520的博客 1万+

基于STM32的智能浇花系统毕业设计(全部资料)

摘要:本文设计了一种基于STM32单片机的智能浇花系统,集成了环境温湿度检测、土壤温湿度监测、LCD显示、Wi-Fi联网及手机APP远程控制等功能。系统通过传感器实时采集环境参数,自动控制水泵工作实现智能灌溉,同时支持用户通过手机APP远程监控和调节。论文详细阐述了硬件选型、软件设计及系统测试过程,验证了系统的稳定性和实用性。该设计为家庭园艺提供了智能化解决方案,具有节水高效、操作便捷等特点。关键词:STM32;智能灌溉;远程控制;环境监测;Wi-Fi通信。

3254968421的博客 1194

如何优雅地打印Java数组?

在逛 Stack Overflow 的时候,发现了一些访问量像‎安第斯山一样高的问题,比如说这个:打印 Java 数组最优雅的方式是什么?访问量足足有 220W+,想不到啊,这么简单的问题竟然有这么多程序员被困扰过。 来回顾一下提问者的问题吧: 在 Java 中,数组虽然是一个对象,但并未明确的定义这样一个类,因此也就没有覆盖 toString() 方法的机会。如果尝试直接打印数组的话,输出的结...

沉默王二 2万+

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

多维数组及for-each循环

一、多维数组 1、数组的创建 多维数组可以看成是数组的数组,比如二维数组就是一个特殊的一维数组,其每一个元素都是一个一维数组. 声明二维数组的语法:数据类型 [ ][ ]数组名; 数组的初始化:数组名=new 数据类型[length1][length2]; 注意:使用语法new int[5][ ]创建数组时,必须指定一个下标。语法new int[ ][ ]是错误的。 例如: 1.//创建一个5...

weixin_43294413的博客 1706

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Java数组 和 对象的打印方法

一、数组的打印方法 1、一维数组 int[] array = {1,2,3,4,5}; (1)传统的for循环方式 for(int i=0;i<array.length;i++) { System.out.println(a[i]); } (2)for each循环 for(int a:array) System.out.println(a); (...

wangpailiulanqi8的博客 3118

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

java中打印数组的方法_Java数组方法–如何在Java中打印数组

java中打印数组的方法An array is a data structure used to store data of the same type. Arrays store their elements in contiguous memory locations. 数组是用于存储相同类型数据的数据结构。 数组将其元素存储在连续的内存位置中。 In Java, arrays are o...

cumian9828的博客 4317

Java循环及数组

一、循环 \quadJava的for,while循环与C/C++及其相似,不同之处在于Java提供了更强大的跳出循环的功能。在Java中,break不仅可以跳出当前循环,还可以跳出更外层指定循环层的循环,此时需要在break后加一个标签,这个标签用于标识一个外层循环。Java中的标签就是一个紧跟英文冒号的标识符,且放在循环语句前。示例程序如下: public static void main(St...

qq_40438165的博客 2489

Java中打印数组的几种方法

Java中打印数组的几种方法

伟迷不正的博客 2万+

Java 打印数组

在 Java 中,我们经常需要打印数组的内容以便进行调试或输出结果。下面介绍几种常见的打印数组的方法。方法1:使用for循环遍历数组并打印每个元素。如果是多维数组的话,使用多层 for 循环嵌套就可以打印了。登录后复制 int[] array = {1, 2, 3, 4, 5}; for (int i = 0; i &...

weixin_42303285的博客 1622

JAVA打印数组的四种方法

JAVA打印数组的四种方法探究 无聊看模块Integer.java源码时(IDEA中鼠标悬浮在Integer上按ctrl+alt 可以转到类源码),因为刚学java,想尝试着仿照python的方法打印数组: final int[] arr1 = {1,2,3,4,5}; System.out.println(arr1); 只打印出了地址: 于是上网搜了下打印数组内容的正确方法,整理如下: 1.循环打印数组 String [] arr2 = {"dashi", "baiqun", "wangxiang"

7ruth0hn的博客 1万+

Java数组的三种打印方式

1.数组的输出的三种方式 一维数组: 定义一个数组 int[] array = {1,2,3,4,5}; (1)传统的for循环方式 for(int i=0;i<array.length;i++) { System.out.println(array[i]); } (2)for each循环 for(int a:array) System.out...

chenkaibsw的专栏 36万+

java多维数组

多维数组的介绍、创建、初始化以及打印方式

萌萌白白的博客 505

Java 数组 、For-Each循环

文章目录前言数组的定义一、声明数组变量二、创建数组三、处理数组For-Each 循环数组作为函数的参数数组作为函数的返回值多维数组多维数组的动态初始化(以二维数组为例)多维数组的引用(以二维数组为例)Arrays 类 前言 数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同。 Java 语言中提供的数组是用来存储固定大小的同类型元素。 数组的定义 数组是相同类型数据的有序集合。 数组描述的是相同类型的若干个数据,按照一定的先后次序排列组合而成。 其中,每个数据称做

diaolove的博客 453

java用for循环输出数组,Java打印输出数组的三种方式:传统的for循环方式,for each循环,利用Arrays类中的toString方法...

Java打印输出数组的三种方式:传统的for循环方式,for each循环,利用Arrays类中的toString方法1.数组的输出的三种方式一维数组:定义一个数组 int[] arr = {1,2,3,4,5};(1)传统的for循环方式for(int i=0;iSystem.out.println(arr[i]);}(2)for each循环for(inta:arr){System.out.p...

weixin_42518930的博客 3850

Java数组的打印

java数组的打印

jiuweijian的博客 2478
上一篇: 关于unity(c#)调用shell脚本
下一篇: 魔方还原
慕一春
博客等级 码龄12年 19粉丝 75原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值