我遇到以下问题:我需要一个有效的Java程序来生成-100到99999个分类间隔范围。
Example: A = -10 to 100, B = 200-300...
例外:
///编辑 我很抱歉,我正在使用google lang来沟通,大部分仍在使用haha 所以代码与我现在在工作中所做的非常相似,但当然我无法深入讨论哈哈 我想要快速或简单的东西
package com.teste;
import java.util.ArrayList;
import java.util.List;
//It is for java 1.4
public class Main {
public static void main(String[] args) {
try {
//These numbers are provided by user
Intervalo a = new Intervalo("20", "50");
//The lowest number will define the first num of interval
//In this case -20
Intervalo b = new Intervalo("-20", "150");
//Invalid interval, in conflict with 'a'
Intervalo c = new Intervalo("10", "30");
Intervalo d = new Intervalo("40", "10");//Will throw 2
Intervalo e = new Intervalo("100","200");
//Then the range of interval is -20 to 99999
//But the parameters are dynamic so i did the follow
List lista = new ArrayList();
int menorNum = 99999;
lista.add(a);
// lista.add(b);
// lista.add(c);
// lista.add(d);
lista.add(e);
//Now, I need to validade
//1-I need to fill all numbers x to 99999 otherwise throw some exception like "there is numbers to categorize..."
//2-throw exception if getIntervalo2()<getIntervalo1();
//3-throw exception if one interval are in other interval of numbers
for (int i = 0; i < lista.size(); i++) {
Intervalo intervalo = (Intervalo) lista.get(i);
int num1 = Integer.parseInt(intervalo.getIntervalo1());
int num2 = Integer.parseInt(intervalo.getIntervalo2());
if(num2<num1){
throw new Exception("Exception 2...");
}
}
//finally the lowest number
for (int i = 0; i < lista.size(); i++) {
Intervalo intervalo = (Intervalo) lista.get(i);
if(Integer.parseInt(intervalo.getIntervalo1())<menorNum){
menorNum = Integer.parseInt(intervalo.getIntervalo1());
}
}
List listaNumeros = new ArrayList();
for (int i = menorNum; i < 100000; i++) {
listaNumeros.add(i);
}
//I was thinking to make listaNumeros something imutable,
// copying to new List, but i don't know if it is worth about memory usage
for (int i = 0; i < lista.size(); i++) {
Intervalo intervalo = (Intervalo) lista.get(i);
int num1 = Integer.parseInt(intervalo.getIntervalo1());
int num2 = Integer.parseInt(intervalo.getIntervalo2());
int range = num2 - num1;
int indexOfNum1 = listaNumeros.indexOf(num1);
if(indexOfNum1==-1){
//Throw the exception 3
throw new Exception("Exception 3... numbers already in use");
}else{
for (int k = indexOfNum1; k <=range; k++) {
listaNumeros.set(k, null);//The number already used haha...
}
}
}
//To exception 1
//Copying again
List listOfExceptions = new ArrayList();
String[] interval = new String[2];
//My idea is, [1,2,3,null,null,4,...], then i know that left to fill [1-3] and [4-99999]
for (int i = 0; i < listaNumeros.size(); i++) {
if(listaNumeros.get(i)!=null&&interval[0]==null){
interval[0]=listaNumeros.get(i)+"";//I forget what i dided in this line but in my work '+' to concat String are prohibited haha ;
}
if(listaNumeros.get(i)==null&&interval[0]!=null){
interval[1]=listaNumeros.get(i-1)+"";
}
if(i==listaNumeros.size()-1){
interval[1]=listaNumeros.get(i)+"";
}
if(interval[0]!=null&&interval[1]!=null){
listOfExceptions.add(new Exception("Left interval "+interval[0]+" - "+interval[1]));
interval = new String[2];
}
}
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < listOfExceptions.size(); i++) {
Exception ex = (Exception) listOfExceptions.get(i);
sb.append(ex.getMessage()).append("\n");
}
if(!sb.toString().equals("")){
throw new Exception(sb.toString());
}
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(":-D");
}
}
}
答案 0 :(得分:1)
我不确定你是否已经尝试过这个,但你可以尝试这样的事情,我知道这不是最好的方法,但应该有效。
for(int i = -100; i< 99999; i++ ){
if(i>=-10 && i<=100){
System.out.println("A");
}
else if(i>=200&&i<=300){
System.out.println("B");
}
// more else ifs
else{
System.out.println("Catch");
}
}
如果这有帮助的话。
答案 1 :(得分:0)
根据original revision of the question和提供的代码,在我看来问题是:给定一系列间隔,验证这些间隔满足3个条件。我正在解决下面这个问题。
我不打算给你代码,但是这里的想法是:
sort the intervals
if first.start != -100
exception - not filling all numbers
iterate through the sorted intervals
if current.end > current.start
exception - invalid interval
if previous.end + 1 < current.start
exception - not filling all numbers
if previous.end + 1 > current.start
exception - overlapping intervals
if last.end != 99999
exception - not filling all numbers