Java翻转数组的方法

java的api没用翻转数组的工具类,只能自己写了。

 int [] testIntArr = {1,2,3};
//翻转数组
for (int i = 0; i < testIntArr.length / 2; i++) {
     Object temp = testIntArr[i];
      testIntArr[i] = testIntArr[testIntArr.length - 1 - i];
      testIntArr[testIntArr.length - 1 - i] = (int) temp;
            }
         for (int i = 0; i < testIntArr.length; i++) {
            System.out.println(testIntArr[i]);  // 321
        }

原文地址:https://www.cnblogs.com/luguankun/p/10432249.html

时间: 2024-08-01 12:43:43

Java翻转数组的方法的相关文章

Java入门——数组和方法

练习题 求100——1000内的水仙花数 1 public class Shuixianhua { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 int count=1; 9 for(int i=100;i<=1000;i++){ 10 int a=(i-i%100)/100; 11 int b=(i-100*a)/10;

将java中数组转换为ArrayList的方法实例(包括ArrayList转数组)

方法一:使用Arrays.asList()方法 1 2 String[] asset = {"equity", "stocks", "gold", "foreign exchange","fixed income", "futures", "options"}; List<String> assetList = Arrays.asList(asset);

去掉有序数组中重复数字 原地 leetcode java (最简单的方法)

1.利用荷兰国旗的思路,每次记住最后一个位置,遇到一个不重复的数,放在它后面,代码很简单. Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with consta

JAVA遍历二位数组的方法

//使用方法对于二维数组进行遍历 package com; import java.util.Arrays; public class CompoundInterest { public static void main(String[] args) { // TODO Auto-generated method stub double[][] balances= new double[3][3]; for(int j = 0; j < balances[0].length; j++){ bal

Java中的数组和方法

3.1 数组的定义和使用 数组(Array)是用来存储一组相同数据类型数据的集合.数组中的每个数据称为一个元素(element),数组可以分为一维数组,二维数组和多维数组.我们 主要讲解一维数组和二维数组. 3.1.1一维数组的声明数组变量 Java中的数组必须先声明然后再使用,Java中声明数组的方式如下: datatype[] arrayRefVar; 或者 datatype arrayRefVar[]; 例如: double[] array; 或者 double array[]; 说明:我

java中数组有没有length()方法?string没有lenght()方法?

java中数组有没有length()方法,求数组的长度可以使用数组的length属性. int[] arr={1,2,3,4,5}; int length=arr.length;//求数组的长度 ---------------------------------------------------------------------------------------- String 有length()方法,用来求字符串的长度 String  str="Hello"; int leng

【Java】用JDK1.5之后的新型数组遍历方法遍历HashMap、HashMap不应该存储多元组

在JDK1.5就多了一种for循环的遍历写法,现在应该没有人用JDK1.4了吧?我见那些2005年出的JAVA书,谭浩强系列的JAVA书都是使用JDK1.5了,当然JDK1.7已经使用的,据说JDK1.7是兼容JDK1.2~JDK1.7,现在的JAVA编程都是基于JDK1.5的.然而由于考试不要求或者其它什么原因,它并不受程序猿的青睐,而在平常实践用,旧式的循环中用多了,老程序猿依旧作为开发主力的情况,也就没有人敢于尝试这种新型的数组遍历方法了,其实这种方法在遍历HashMap的时候尤其有用,能

乐字节Java反射之三:方法、数组、类加载器和类的生命周期

本文承接上一篇:乐字节Java发射之二:实例化对象.接口与父类.修饰符和属性 继续讲述Java反射之三:方法.数组.类加载器 一.方法 获取所有方法(包括父类或接口),使用Method即可. public static void test() throws Exception { Class<?> clz = Class.forName("com.shsxt.ref.simple.User "); //获取属性 System.out.println("======

Java中数组的特性

转载:http://blog.csdn.net/zhangjg_blog/article/details/16116613 数组是基本上所有语言都会有的一种数据类型,它表示一组相同类型的数据的集合,具有固定的长度,并且在内存中占据连续的空间.在C,C++等语言中,数组的定义简洁清晰,而在Java中确有一些会让人迷惑的特性.本文就尝试分析这些特性. Java中的数组是对象吗? Java和C++都是面向对象的语言.在使用这些语言的时候,我们可以直接使用标准的类库,也可以使用组合和继承等面向对象的特性