java中交换两个数的方法是什么  api有吗?

解决方案 »

  1.   

    整数么? 不用api吧 x = x^y;
     y = y^x;
     x = x^y;
      

  2.   

    3行代码呀...不多的干嘛还要用个类包起来int temp=a;
    a=b;
    b=temp;over....
      

  3.   

    不可能,因为Java没有指针,像C那种进行交换
      

  4.   

    int x = 1;
    int y = 2;x = x+y;
    y = x-y;
    x = x-y;
      

  5.   

    int x = 1;
    int y = 2;x = x+y;
    y = x-y;
    x = x-y;
      

  6.   

    各种方法,最简单的就是temp,不需要封装吧。要不你自己封装下好了。
      

  7.   

    int x = 1;
    int y = 2;x = x+y;
    y = x-y;
    x = x-y;
      

  8.   

    int a=3;
    int b=4;
    a=b+(b=a)*0;
      

  9.   

    楼上那么多人都是新手吗java 里没有指针,不可能用个方法把两个数进行交换的
      

  10.   

    两个方法
    一 int x = 1; int y = 2;
       x = x + y;
       y = x - y;   y变为x
       x = x - y;   x变为y二 int x = 1; int y = 2; int temp = 0;
       temp = x;
       x = y;    x变为y
       y = temp; y变为x
      

  11.   

    int temp=a;
    a=b;
    b=temp;
      

  12.   

    用一个堆栈就可以了
    先将两个数存进去,在pop出来就是对换之后的了
      

  13.   

    Collections.swap(List<?> list, int i, int j)
    ?LZ不会说的是这个吧
      

  14.   


    只是单纯的交换,而不是什么写个方法调用什么的还是可以的吧比如这个int a=1, b=2;
    a^=b;b^=a;a^=b;但是下面这样是不可以int a=1, b=2;
    swap(a,b);void swap(int a, int b) {
        a^=b;b^=a;a^=b;
    }
      

  15.   

    方法这个词有歧义,是 Java 里面的 Method 还是 "办法" 的意思?
    如果是 Java 里面的 Method,那没有,“办法”倒是有。上面的 + / - 的办法不好,可能超出精度范围,不如用 ^.^
      

  16.   

    最简单的交换两个值得方法就是 用数组对象存储,然后交换数组对象中的元素位置
    //ChangeTheNumber1
    public class ChangeTheNumber1{
       public static void main(String args[]){
          int[] a=new int[5];
          initize(a);
          int i=1,j=2;
          Swap(a,i,j);
          System.out.println(a[i]+","+a[j]);}
          public static void Swap(int[] x,int i,int j)//用数组可以实现交换2个元素的值
          {
             int temp=x[i];
             x[i]=x[j];
             x[j]=temp;
             }
         public static void initize(int[] x){
          for(int i=1;i<=5;i++)
       {
          x[i-1]=i;
          }
          }    
          
       }
      

  17.   

    看API,有:
    java.util.Collections.swap(List<?> list, int i, int j);
    这个Collections是集合框架里的一个类,它把实现了List接口的数据中的i元素和j元素互换。
    你所谓的是个类似的功能吧。以此来看,建议你实现自己的swap方法,它是泛型的,可以接受任何类型。
    为了实现这个swap,需要准备一个容器来装载需要交换的两个数据,
    因为JAVA得方法调用中,参数的传递是 --值传递-- 所以不能用类似C的办法来实现。
    当然了,应用泛型有个好处,避免错误的类型转换。
    代码如下(大部分来自《JAVA核心技术》,用《Effective Java》优化过):/*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package mytester;/**
     *
     * @author Rains.C
     */
    public class Main {  /**
       * @param args the command line arguments
       */
      public static void main(String[] args) {
        Pair<Integer> p = new Pair(1, 2);
        System.out.println(p);
        System.out.println("After swap:");
        System.out.println(p.swap());
      }
    }class Pair<T> {
      private Pair() throws CreationException{throw new CreationException();}
      public Pair(T first, T second) {this.first = first;this.second = second;}  public void setFirst(T newValue) {first = newValue;}
      public T getFirst() {return first;}  public void setSecond(T newValue) {second = newValue;}
      public T getSecond() {return second;}  public Pair swap() {
        T temp = second;
        second = first;
        first = temp;
        return this;
      }  @Override
      public String toString() {
        return first + " | " + second;
      }  private T first;
      private T second;  private static class CreationException extends Exception {
        public CreationException() {
          super("Don't use this Creator");
        }
      }
    }