This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fjr.test.testgeneric; | |
public class A { | |
String name; | |
public A(String name){ | |
this.name = name; | |
} | |
public String toString(){ | |
return name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fjr.test.testgeneric; | |
public class B extends A{ | |
int id; | |
public B(String name , int id){ | |
super(name); | |
this.id = id; | |
} | |
public String toString(){ | |
return super.toString()+ " | " + Integer.toString(id); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fjr.test.testgeneric; | |
import java.util.ArrayList; | |
public class TestConvert { | |
public TestConvert(){ | |
ArrayList<A> listA = new ArrayList<>(); | |
for(int i=0; i < 10; i++){ | |
listA.add(new A("anak "+Integer.toString(i))); | |
} | |
ArrayList<B> list = convert(listA); | |
for(int i=list.size() - 1; i>=6; i--){ | |
list.remove(i); | |
} | |
for(int i=0; i< list.size(); i++){ | |
System.out.println(list.get(i)); | |
} | |
} | |
public ArrayList<B> convert(ArrayList<A> listA){ | |
ArrayList<B> listHasil = (ArrayList<B>) listA; | |
return listHasil; | |
} | |
public static void main(String[] args){ | |
new TestConvert(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fjr.test.testgeneric; | |
import java.util.ArrayList; | |
public class TestConvert { | |
public TestConvert(){ | |
ArrayList<A> listA = new ArrayList<>(); | |
for(int i=0; i < 10; i++){ | |
listA.add(new A("anak "+Integer.toString(i))); | |
} | |
ArrayList<B> list = convert(listA); | |
for(int i=list.size() - 1; i>=6; i--){ | |
list.remove(i); | |
} | |
for(int i=0; i< list.size(); i++){ | |
System.out.println(list.get(i)); | |
} | |
} | |
public <T> ArrayList<T> convert(ArrayList<A>listA){ | |
ArrayList<T> listHasil = (ArrayList<T>)listA; | |
return listHasil; | |
} | |
public static void main(String[] args){ | |
new TestConvert(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fjr.test.testgeneric; | |
import java.util.ArrayList; | |
public class TestConvert { | |
public TestConvert(){ | |
ArrayList<A> listA = new ArrayList<>(); | |
for(int i=0; i < 10; i++){ | |
listA.add(new A("anak "+Integer.toString(i))); | |
} | |
ArrayList<String> list = convert(listA); | |
for(int i=list.size() - 1; i>=6; i--){ | |
list.remove(i); | |
} | |
for(int i=0; i< list.size(); i++){ | |
System.out.println(list.get(i)); | |
} | |
} | |
public <T> ArrayList<T> convert(ArrayList<A>listA){ | |
ArrayList<T> listHasil = (ArrayList<T>)listA; | |
return listHasil; | |
} | |
public static void main(String[] args){ | |
new TestConvert(); | |
} | |
} |