java中使用数组过滤ipv4地址
我必须过滤Ipv4的IP地址(32位),但当我使用高达32位的数组索引{array[32位]}时,它会给出错误。因此,我将ip分为三部分:; 1-1部分包含25位{数组[25位]}的索引值 2-第二部分包含5位的值,该值使用5到32解码器解码。 3-两位的第三部分表示4个不同的数组,其中5位解码值存储在25位地址位置。 它在第四个短数组上给出了一个错误。 当我将地址减少到24位并解码5位以存储在8个不同的短数组中时,这里3位从8个短数组中选择数组,然后它在第8个短数组中也给出了一个错误, “线程中的异常”main“java.lang.OutOfMemoryError:java堆空间” 如何解决这个问题? 是否有其他数据结构(如linkedlist、tree等)可以解决此问题 导入java.util.Scanner; 类IP地址3{java中使用数组过滤ipv4地址,java,arrays,Java,Arrays,我必须过滤Ipv4的IP地址(32位),但当我使用高达32位的数组索引{array[32位]}时,它会给出错误。因此,我将ip分为三部分:; 1-1部分包含25位{数组[25位]}的索引值 2-第二部分包含5位的值,该值使用5到32解码器解码。 3-两位的第三部分表示4个不同的数组,其中5位解码值存储在25位地址位置。 它在第四个短数组上给出了一个错误。 当我将地址减少到24位并解码5位以存储在8个不同的短数组中时,这里3位从8个短数组中选择数组,然后它在第8个短数组中也给出了一个错误, “线程
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");
}
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入要从列表中搜索的IP地址:”);
字符串ipAddress=input.nextLine();
//字符串ipAddress=“171.255.100.23”;
字符串[]八位字节=ipAddress.split(“\\”);
StringBuilder sb=新的StringBuilder();
for(字符串八位字节:八位字节){
int number=整数.parseInt(八位字节);
如果(数字>255)
{
System.out.println(“错误:无效Ip地址,\n”+
“每个八位字节应小于或等于255\n”);
系统出口(0);
}
否则{
字符串binaryPart=Integer.toBinaryString(数字);
if(binaryPart.length()<8){
对于(int i=binaryPart.length();i<8;i++){
binaryPart=“0”+binaryPart;
}
}
某人附加(二进制部分);
}
}
字符串binaryForm=sb.toString();
System.out.println(二进制格式);
System.out.println(“2 MSB:+binaryForm.substring(0,2));
System.out.println(“5位:+binaryForm.substring(2,7));
System.out.println(“25lsb:+binaryForm.substring(7));
字符串MSB2Bit=binaryForm.substring(0,2);
stringfivebits=binaryForm.substring(2,7);
String twenty5bits=binaryForm.substring(8);
字节msb2Bit=byte.parseByte(msb2Bit);
字节数据=byte.parseByte(五位,2);
int address=Integer.parseInt(二十五位,2);
System.out.println(“数据:\t”+数据);
System.out.println(“地址:“+地址”);
short[]阵列0=新的short[33554432];
短[]阵列1=新短[33554432];
short[]Array2=新的short[33554432];
short[]Array3=new short[33554432];//此行出错
/*
short[]阵列0=新的short[16777216];
短[]阵列1=新短[16777216];
short[]Array2=新的short[16777216];
short[]Array3=新的short[16777216];
short[]Array4=新的short[16777216];
短[]阵列5=新短[16777216];
短[]阵列6=新短[16777216];
short[]Array7=新的short[16777210];//此处出错
/*
阵列0[1677721]=127;
阵列1[1677721]=127;
阵列2[1677721]=127;
int thirty2BitWord=0;//Array0[ad]=Array0[ad]| Arra0[ad];
int filterWord=8;
交换机(数据)
{
案例0:
thirty2BitWord=1;
打破
案例1:
thirty2BitWord=2;
打破
案例2:
三十个字=4;
打破
案例3:
三十二字=8;
打破
案例4:
thirty2BitWord=16;
打破
案例5:
三十二个字=32;
打破
案例6:
三十个字=64;
打破
案例7:
thirty2BitWord=128;
打破
案例8:
thirty2BitWord=256;
打破
案例9:
thirty2BitWord=512;
打破
案例10:
三十个字=1024;
打破
案例11:
thirty2BitWord=2048;
打破
案例12:
thirty2BitWord=4096;
打破
案例13:
thirty2BitWord=8192;
打破
案例14:
thirty2BitWord=16384;
打破
案例15:
thirty2BitWord=32768;
打破
案例16:
thirty2BitWord=65536;
打破
案例17:
thirty2BitWord=131072;
打破
案例18:
thirty2BitWord=262144;
打破
案例19:
thirty2BitWord=524288;
打破
案例20:
thirty2BitWord=1048576;
打破
案例21:
thirty2BitWord=2097152;
打破