次の問題があります: Java で -100 から 99999 の範囲のカテゴリ化間隔を生成する効果的なプログラムが必要です。
Example: A = -10 to 100, B = 200-300...
例外:
- 範囲を重複させることはできません。
- すべての数字を入力する必要があります。
- 対応する数は最初の数よりも小さくすることはできません
///編集申し訳ありませんが、私はコミュニケーションにGoogle言語を使用していましたが、大部分はまだ使用しています(笑)したがって、コードは私が現在仕事で行っていることと非常に似ていますが、もちろん深く話すことはできません(笑)迅速または簡単なもの
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");
}
}
}