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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 夜空中最亮的星 中级黑马   /  2015-6-28 09:52  /  619 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

定义一个包含10个元素的数组,对其进行赋值,使每个元素的值等于其下标,
然后输出,最后将数组倒置后输出
  1. public class ArraryTest{
  2.         public static void main(String[] args){
  3.                 int array[]=new int[10];
  4.                 arrayAssignment(array);
  5.                 prin(array);
  6.                 inveArray(array);
  7.                 prin(array);
  8.         }       

  9. //函数:为数组赋值,使其元素值等于其下标
  10.         public static void arrayAssignment(int temp[]){
  11.                 for(int i=0;i<temp.length;i++){
  12.                         temp[i]=i;
  13.                 }
  14.         }

  15. //函数:倒置数组元素
  16.         public static void inveArray(int temp[]){
  17.                 int tempArray[]=new int[temp.length];  //定义一个临时数组来存放倒置的数据
  18.                 int j=temp.length-1;                   //临时数组的下标
  19.                         for(int i=0;i<temp.length;i++){
  20.                         tempArray[j] = temp[i];
  21.                         j--;
  22.                 }
  23.                 for(int i=0;i<temp.length;i++){
  24.                         temp[i]=tempArray[i];
  25.                 }
  26.         }

  27. //函数:输出数组
  28.         public static void prin(int temp1[]){
  29.                 for(int i=0;i<temp1.length;i++){
  30.                         System.out.print(temp1[i]+" ");
  31.                 }
  32.                 System.out.println();
  33.         }
  34. }
复制代码




4 个回复

倒序浏览
好牛逼的样子:funk:
回复 使用道具 举报
谢谢分享
回复 使用道具 举报
徐家誉 来自手机 中级黑马 2015-6-28 10:52:19
板凳
谢谢分享
回复 使用道具 举报
顶一个!!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马