chris_head
Mitglied
Ich danke Dir. So ähnlich werde ichs benutzen.
Folge dem Video um zu sehen, wie unsere Website als Web-App auf dem Startbildschirm installiert werden kann.
Anmerkung: Diese Funktion ist in einigen Browsern möglicherweise nicht verfügbar.
/**
*
*/
package de.tutorials;
import java.lang.reflect.Array;
import java.lang.reflect.ParameterizedType;
/**
* @author Thomas.Darimont
*/
public class IndividualExample {
public static void main(String[] args) {
Individual<Integer> individual0 = new Individual<Integer>(5) {
};
individual0.set(0, 1);
individual0.set(1, 2);
individual0.set(2, 3);
individual0.set(3, 4);
individual0.set(4, 5);
System.out.println(individual0);
Individual<Integer> individual1 = new Individual<Integer>(new Integer[] { 1, 2, 3, 4, 5 });
System.out.println(individual1);
}
static class Individual<TGenoType> implements Comparable<Individual<TGenoType>> {
double fitness;
int size;
Class<?> type;
TGenoType[] genoType;
@SuppressWarnings("unchecked")
public Individual(int size) {
this.size = size;
this.type = (Class<?>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
this.genoType = (TGenoType[]) Array.newInstance(this.type, size);
}
public Individual(TGenoType[] genoType) {
this.genoType = genoType;
this.size = genoType.length;
type = genoType.getClass().getComponentType();
}
public double getFitness() {
return fitness;
}
public void setFitness(double fitness) {
this.fitness = fitness;
}
public TGenoType get(int index) {
return this.genoType[index];
}
public void set(int index, TGenoType value) {
this.genoType[index] = value;
}
public TGenoType[] get() {
return this.genoType;
}
public void set(TGenoType[] genoType) {
this.genoType = genoType;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int compareTo(Individual<TGenoType> o) {
return (int) (getFitness() - o.getFitness());
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
int currentIndex = 0;
for (TGenoType genoType : this.genoType) {
stringBuilder.append(genoType);
if ( ++currentIndex < this.genoType.length) {
stringBuilder.append(",");
}
}
return stringBuilder.toString();
}
}
}
public enum Genotype {
Binary("Binary String",Boolean.class),
Real("Real Value String",Double.class);
private String name;
private Object dataType = null;
Genotype(String name,Object dataType){
this.name = name;
this.dataType = dataType;
...
public Object getDataType(){
return datatype;}
...
Individual<Boolean> ind = new Individual<Boolean>(16);
Individual<Boolean> ind = new Individual<genotype.getDataType()>(16);