public class Swap
{
public static void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
public static void main(String[] args)
{
int x = 3;
int y = 4;
Swap.swap(x, y);
System.out.println(x);//3
System.out.println(y);//4
System.out.println("111111111111");
int temp = x;
x = y;
y = temp;
System.out.println(x);//4
System.out.println(y);//3
System.out.println("2222222222222");
int a = 3;
int b = 4;
a = a + b;
b = a - b;
a = a - b;
System.out.println(a);//4
System.out.println(b);//3
}
}
public class Swap2
{
public static void swap(char[] ch, char c)
{
ch[0] = 'B';
c = 'D';
}
public static void main(String[] args)
{
char[] ch = {'A', 'C'};
swap(ch, ch[1]);
for(int i = 0; i < ch.length; i++)
{
System.out.print(ch[i] + " ");//B C
}
}
}
public class Swap3
{
public static void swap(int[] i)
{
int temp = i[0];
i[0] = i[1];
i[1] = temp;
}
public static void main(String[] args)
{
int[] i = {1, 2};
swap(i);
System.out.println(i[0]);//2
System.out.println(i[1]);//1
}
}
public class ArrayTest6
{
public static void main(String[] args)
{
I[] i = new I[2];
i[0] = new C();
i[1] = new C();
int[] a = new int[] {1, 2, 3};//原生数据类型直接赋值
I[] b = new I[]{new C(), new C()};//等价于I[] i = new I[2]; i[0] = new C(); i[1] = new C();
}
}
interface I
{
}
class C implements
{
}
public class ArrayEqualsTest
{
public static boolean isEquals(int[] a, int[] b)
{
if(a == null || b == null)
{
return false;
}
if(a.length != b.length)
{
return false;
}
for(int i = 0; i < a.length; i++)
{
if(a[i] != b[i])
{
return false;
}
}
return true;
}
public static void main(String[] args)
{
int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
System.out.println(isEquals(a, b));//true
System.out.println(Arrays.equals(a, b));//true
}
}