A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈冬雪 中级黑马   /  2015-7-31 23:08  /  656 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package cn.itcast;

  2. public class Array {

  3.         Array(){}
  4.        
  5.         public void printArray(int[] arr){
  6.                 //int[] arr;
  7.                 int len=arr.length;
  8.                
  9.                 if(len==0){
  10.                         System.out.println("[]");
  11.                 }
  12.                
  13.                 else        if(len==1){
  14.                         System.out.println("["+arr[0]+"]");
  15.                 }
  16.        
  17.                 else {
  18.                         System.out.print("["+arr[0]);
  19.                         for (int i = 1; i < arr.length; i++) {
  20.                                 System.out.print(","+arr[i]);
  21.                         }
  22.                         System.out.println("]");
  23.                 }
  24.                
  25.         }


  26. }
复制代码

10 个回复

倒序浏览
看一遍,你是要按格式输出数组中的元素。
回复 使用道具 举报
青春是你的烟火 发表于 2015-7-31 23:12
看一遍,你是要按格式输出数组中的元素。

学霸说的对!!!!
回复 使用道具 举报
陈冬雪 发表于 2015-7-31 23:21
学霸说的对!!!!

等……给你写一个我自认为标准的……
回复 使用道具 举报
青春是你的烟火 发表于 2015-7-31 23:26
等……给你写一个我自认为标准的……

好的。。。{:3_53:}
回复 使用道具 举报
  1. package cn.itcast;

  2. public class ArrayDemo {

  3.         public String print(int[] arr) {
  4.                 String temp = "\"[";
  5.                 for (int i = 0; i < arr.length; i++) {
  6.                         // 如果是第一个元素,就不加空格
  7.                         if (i == 0) {
  8.                                 temp += arr[i];
  9.                         } else {
  10.                                 temp += " " + arr[i];
  11.                         }
  12.                 }
  13.                 return temp + "]\"";
  14.         }

  15.         /**
  16.          * 返回查找元素的位置,如果没有就返回-1
  17.          */
  18.         public int find(int[] arr, int index) {
  19.                 int pos = -1;
  20.                 for (int i = 0; i < arr.length; i++) {
  21.                         if (arr[i] == index) {
  22.                                 pos = i;
  23.                                 break;
  24.                         }
  25.                 }
  26.                 return pos;
  27.         }

  28.         public static void main(String[] args) {
  29.                 int[] arr = { 98, 23, 16, 35, 72 };
  30.                 ArrayDemo a = new ArrayDemo();
  31.                 System.out.println(a.print(arr));
  32.                 System.out.println(a.find(arr, 23));

  33.         }

  34. }
复制代码


我这个比较随意……看懂就OK。
回复 使用道具 举报 1 0

给你的不是很好……
回复 使用道具 举报
青春是你的烟火 发表于 2015-7-31 23:29
我这个比较随意……看懂就OK。

一定要看懂,一定
回复 使用道具 举报
chaoqi 中级黑马 2015-7-31 23:48:49
9#
太棒了  这么厉害 攒攒赞
回复 使用道具 举报
看过了,还不错
回复 使用道具 举报
赞赞   加油哦
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马