0

我必须过滤 Ipv4(32 位)的 IP 地址,但是当我使用高达 32 位的数组索引 {array[32 位]} 时会出错。所以我将ip分为三个部分:1-一部分包含 25 位的索引值 {array[25 位]} 2-第二部分包含 5 位的值,该值使用 5 到 32 解码器解码。3-两位的第三部分表示4个不同的数组,其中5位解码值存储在25位地址位置。它在第四个短数组上给出错误。当我将地址减少到 24 位并将 5 位解码以存储在 8 个不同的短数组中时,这里 3 位从 8 个中选择数组,那么它也会在第八个短数组上给出错误, “线程“主”java.lang 中的异常。 OutOfMemoryError:Java 堆空间”

如何解决这个问题?是否有任何其他数据结构,如链表、树等可以解决这个问题?

导入 java.util.Scanner;类 IPAddress3 {

public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
    System.out.println("Enter IP address to search from list:");
    String ipAddress=input.nextLine();
    //String ipAddress="171.255.100.23";
    String[] octets = ipAddress.split("\\.");
    StringBuilder sb = new StringBuilder();
    for (String octet : octets) {

            int number = Integer.parseInt(octet);
            if(number>255)
            {
                System.out.println("Error:Invalid Ip address,\n" +
                        "Each octet should be less or equal to 255\n");
                System.exit(0);
            }
            else{
            String binaryPart = Integer.toBinaryString(number);
            if (binaryPart.length() < 8) {
                for (int i = binaryPart.length(); i < 8; i++) {
                    binaryPart = "0" + binaryPart;
                }
            }
            sb.append(binaryPart);
            }
    }
    String binaryForm = sb.toString();
    System.out.println(binaryForm);
    System.out.println("2 MSB: " + binaryForm.substring(0, 2));
    System.out.println("5 bits: " + binaryForm.substring(2, 7));
    System.out.println("25 LSB: " + binaryForm.substring(7));

    String MSB2Bit=binaryForm.substring(0, 2);
    String fivebits= binaryForm.substring(2, 7);
    String twenty5bits=binaryForm.substring(8);
    byte msb2Bit=Byte.parseByte(MSB2Bit);
    byte data=Byte.parseByte(fivebits,2);
    int address=Integer.parseInt(twenty5bits,2);
    System.out.println("Data:\t"+data);
    System.out.println("Address: "+address);



    short[] Array0=new short[33554432];
short[] Array1=new short[33554432];
    short[] Array2=new short[33554432];
    short[] Array3=new short[33554432];//error in this line

   /* 
    short[] Array0=new short[16777216];
    short[] Array1=new short[16777216];
    short[] Array2=new short[16777216];
    short[] Array3=new short[16777216];
    short[] Array4=new short[16777216];
    short[] Array5=new short[16777216];
    short[] Array6=new short[16777216];
    short[] Array7=new short[16777210];// error here
    /*


            Array0[1677721]=127; 
            Array1[1677721]=127;
            Array2[1677721]=127;



            int thirty2BitWord = 0 ;// Array0[ad]=Array0[ad] | Arra0[ad];
            int filterWord = 8;


                    switch(data)
                    {   
                            case 0:
                            thirty2BitWord=1;
                            break;

                            case 1:
                            thirty2BitWord=2;
                            break;

                            case 2:
                            thirty2BitWord=4;
                            break;

                            case 3:
                            thirty2BitWord=8;
                            break;
                            case 4:
                            thirty2BitWord=16;
                            break;

                            case 5:
                            thirty2BitWord=32;
                            break;

                            case 6:
                            thirty2BitWord=64;
                            break;

                            case 7:
                            thirty2BitWord=128;
                            break;

                            case 8:
                            thirty2BitWord=256;
                            break;

                            case 9:
                            thirty2BitWord=512;
                            break;

                            case 10:
                            thirty2BitWord=1024;
                            break;

                            case 11:
                            thirty2BitWord=2048;
                            break;

                            case 12:
                            thirty2BitWord=4096;
                            break;

                            case 13:
                            thirty2BitWord=8192;
                            break;

                            case 14:
                            thirty2BitWord=16384;
                            break;

                            case 15:
                            thirty2BitWord=32768;
                            break;

                            case 16:
                            thirty2BitWord=65536;
                            break;

                            case 17:
                            thirty2BitWord=131072;
                            break;

                            case 18:
                            thirty2BitWord=262144;
                            break;

                            case 19:
                            thirty2BitWord=524288;
                            break;

                            case 20:
                            thirty2BitWord=1048576;
                            break;

                            case 21:
                            thirty2BitWord=2097152;
                            break;

                            case 22:
                            thirty2BitWord=4194304;
                            break;

                            case 23:
                            thirty2BitWord=8388608;
                            break;

                            case 24:
                            thirty2BitWord=16777216;
                            break;

                            case 25:
                            thirty2BitWord=33554432;
                            break;

                            case 26:
                            thirty2BitWord=67108864;
                            break;

                            case 27:
                            thirty2BitWord=134217728;
                            break;

                            case 28:
                            thirty2BitWord=268435456;
                            break;

                            case 29:
                            thirty2BitWord=536870912;
                            break;

                            case 30:
                            thirty2BitWord=1073741824;
                            break;

                            case 31:
                            thirty2BitWord=2147483647;
                            break;


                    }

                    if(msb2Bit==0)
                    {
                        filterWord=thirty2BitWord & Array0[address];    
                    }
                    else if(msb2Bit==1)
                    {
                        filterWord=thirty2BitWord & Array1[address];    
                    }
                    else if(msb2Bit==2)
                    {
                        filterWord=thirty2BitWord & Array2[address];    
                    }
                   /* else 
                    {
                        filterWord=thirty2BitWord & Array3[address];
                    }*/

                    byte filterbit;
                    if(filterWord==0)
                    {
                        filterbit=0;

                        System.out.println("No such address is found in blacklist, So Allow\n");
                    }
                    else
                    {
                        filterbit=1;

                        System.out.println("IP match, So block this address\n"); 
                    }


}

}

4

1 回答 1

-1

在短期内,您可以通过增加内存来修复堆大小错误:

如何在 Eclipse 中增加应用程序堆大小?

我没有过多地检查你的代码,因为这个错误只是虚拟机内存不足。可能有更好的方法来做到这一点,但如果程序只需要更多的 RAM,这应该可以解决问题!

于 2013-11-12T16:27:50.060 回答