Untitled
unknown
java
4 years ago
4.6 kB
8
Indexable
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
public class IntegerOverflow {
private static final int MAX = 100; // Integer.MAX_VALUE + 1
private int current;
private List<String> data;
public IntegerOverflow() {
current = 0;
data = new ArrayList<String>(MAX);
for (int i = 0; i < MAX; i++) {
data.add(i, "");
}
}
public static void main(String[] args) {
IntegerOverflow io = new IntegerOverflow();
io.process();
}
private void process() {
System.out.println("Liste im Initialzustand:");
System.out.println(data.toString());
/* [, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ] */
current = 50; // Integer.MAX_VALUE
data.set(current, "Alter Wert");
System.out.println("\nListe nach Setzen eines Wertes auf Position current = " + current);
System.out.println(data.toString());
/* [, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Alter Wert, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ] */
/*
System.out.println(Integer.MIN_VALUE); // -2147483648
System.out.println(Math.abs(Integer.MIN_VALUE)); // -2147483648
System.out.println(Integer.MAX_VALUE); // 2147483647
System.out.println(Math.abs(Integer.MAX_VALUE)); // 2147483647
System.out.println(Integer.MIN_VALUE - 1); // 2147483647
System.out.println(Integer.MAX_VALUE + 1); // -2147483648
*/
int extra = Integer.MAX_VALUE + Math.abs(Integer.MIN_VALUE) + 1;
System.out.println("\nextra-Index: = " + extra);
/* extra-Index: = 0 */
// int extra = Math.abs(Integer.MIN_VALUE) + 1;
// int extra = Integer.MAX_VALUE;
// System.out.println("\nBerechnung des extra-Index durch Integer.MAX_VALUE + Math.abs(Integer.MIN_VALUE) + 1 = " + extra);
setElementToExtraPosition(extra, "Neuer falscher Wert");
System.out.println("\nListe nach Setzen eines Wertes auf Position current (" + current + ") + extra (" + extra + ") = " + (current + extra));
System.out.println(data.toString());
/* Liste nach Setzen eines Wertes auf Position current (50) + extra (0) = 50 */
/* [, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Neuer falscher Wert, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ] */
setElementToExtraPositionMoreSecure(extra, "Neuer Wert More Secure");
System.out.println("\nListe nach Setzen eines Wertes auf Position current (" + current + ") + extra (" + extra + ") = " + (current + extra));
System.out.println(data.toString());
/* Liste nach Setzen eines Wertes auf Position current (50) + extra (0) = 50
[, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Neuer Wert More Secure, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ] */
setElementToExtraPositionSecure(extra, "Neuer Wert Secure");
System.out.println("\nListe nach Setzen eines Wertes auf Position current (" + current + ") + extra (" + extra + ") = " + (current + extra));
System.out.println(data.toString());
/* Liste nach Setzen eines Wertes auf Position current (50) + extra (0) = 50
[, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Neuer Wert Secure, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ] */
}
private void setElementToExtraPosition(int extra, String element) {
// System.out.println("current + extra = " + (current + extra));
if (extra < 0 || current + extra > MAX) {
throw new IllegalArgumentException();
}
data.set(current + extra, element);
}
private void setElementToExtraPositionMoreSecure(int extra, String element) {
if (extra < 0 || current > MAX - extra) {
throw new IllegalArgumentException();
}
data.set(current + extra, element);
}
private void setElementToExtraPositionSecure(int extra, String element) {
BigInteger currentBig = BigInteger.valueOf(current);
BigInteger maxBig = BigInteger.valueOf(MAX);
BigInteger extraBig = BigInteger.valueOf(extra);
if (extra < 0 || currentBig.add(extraBig).compareTo(maxBig) > 0) {
throw new IllegalArgumentException();
}
data.set(current + extra, element);
}
}
Editor is loading...