位运算/构造方法

每日一练:双色球

红球336

篮球161

package day0701;

 

import java.util.Arrays;

 

public class Test1 {

    public static void main(String[] args) {

        ShuangSeQiu s = new ShuangSeQiu();

        System.out.println(Arrays.toString(s.select()));

    }

}

 

package day0701;

 

import java.util.Arrays;

import java.util.Random;

 

public class ShuangSeQiu {

    int[] red = {

            1,2,3,4,5,6,7,8,9,10,

            11,12,13,14,15,16,17,18,19,20,

            21,22,23,24,25,26,27,28,29,30,

            31,32,33

    };

    int[] blue = {

            1,2,3,4,5,6,7,8,9,10,

            11,12,13,14,15,16

    };

 

    public int[] select()

    {

        int[] r = Arrays.copyOf(red, 33);

        int[] b = Arrays.copyOf(blue, 16);

 

        for(int i = 0;i<6;i++)

        {

            int j = new Random().nextInt(r.length - i)+i;

            int t = r[j];

            r[j] = r[i];

            r[i] = t;

        }

 

        int[] result = Arrays.copyOf(r,7);

        result[6] = blue[new Random().nextInt(16)];

        return result;

    }

}

位运算

 

&

|

^异或

~求反

>>右移位

>>>不带符号的右移位

<<左移位

00000000000000000000000001011001

00000000000000000000000000101001 &

———————————–

00000000000000000000000000001001

 

00000000000000000000000001011001

00000000000000000000000000101001 |

———————————–

00000000000000000000000001111001

 

00000000000000000000000001011001

00000000000000000000000000101001 ^

———————————–

00000000000000000000000001110000

 

00000000000000000000000001011001 ~

———————————–

11111111111111111111111110100110

 

00000000000000000000000001011001 >>2

————————————

0000000000000000000000000001011001

 

10000000000000000000000001011001 >>2

————————————

1110000000000000000000000001011001

 

10000000000000000000000001011001 >>>2

————————————

00100000000000000000000000010110

 

10000000000000000000000001011001 <<2

————————————

00000000000000000000000101100100

 

位运算

package day0702;

 

import java.util.Scanner;

 

public class Test1 {

    public static void main(String[] args) {

        System.out.print("输入整数:");

        int n = new Scanner(System.in).nextInt();

        //n = n<<24>>>24;

        n = n & 0x000000FF;

        System.out.println(n);

 

 

    }

}

构造方法

新建对象时,执行的一个特殊方法

 

一个类必须有构造方法

如果不定义,编译器编译时会添加默认构造方法

 

public Soldier(){

        System.out.println(“构造方法“);

    }

 

创建对象只有new是创建对象,new为对象分配内存空间

构造方法不负责构造这个对象

构造方法只是构造一个对象时,执行的一个方法代码

构造方法重载

定义多个不同参数的构造方法

class A {

public A() {}

public A(int) {}

public A(int,double) {}

public A(int,double,Sring) {}

public A(String) {}

}

构造方法可以执行任何运算,完成任何功能

常见用法,用来为成员变量赋值

 

整数四个字节拆分合并

package day0703;

 

import java.util.Arrays;

import java.util.Scanner;

 

public class Test1 {

    public static void main(String[] args) {

        System.out.print("输入整数:");

        int i = new Scanner(System.in).nextInt();

        IntByte obj = new IntByte(i);

 

        System.out.println(obj.getInt());

        System.out.println(Arrays.toString(obj.getBytes()));

    }

}

 

package day0703;

 

import java.util.Arrays;

 

public class IntByte {

    int n;

    byte[] bytes;

 

    public IntByte() {

    }

 

    public IntByte(int n) {

        this.n = n;

        this.bytes = new byte[4];

        int num = 24;

        for (int i =0;i<this.bytes.length;i++)

        {

            this.bytes[i] = (byte)(n>>num);

            num-=8;

        }

    }

 

    public int getInt()

    {

        return n;

    }

 

    public byte[] getBytes()

    {

        return Arrays.copyOf(bytes,4);

    }

}