java - Trying to create and test a comparable arraylist class -



java - Trying to create and test a comparable arraylist class -

here's code have. i'm trying test out bubblesort method..

import java.util.arraylist; import java.util.list; public class myarraylist<t extends comparable<? super t>> extends arraylist<comparable<t>> { private static final long serialversionuid = 1l; private myarraylist<t> mylist; // class list public myarraylist(arraylist<comparable<t>> aslist) { (comparable<t> e: aslist) { mylist.add(e); } } public void bubblesort() { boolean swapped = true; while (swapped) { swapped = false; (int = 0; < this.size() - 1; i++) { if (this.get(i).compareto((t) this.get(i + 1)) > 0) { swapped = true; comparable<t> temp = this.get(i); this.set(i, this.get(i + 1)); this.set(i + 1, temp); } } } } }

in test class, having problem creating lists test bubblesort method. want seek create integer list (from array), string list, , few others, maintain getting various errors.

import static org.junit.assert.*; import java.util.arraylist; import java.util.arrays; import java.util.collection; import org.junit.test; public class myarraylisttest<t> { public void setup() { integer[] testintarray = new integer[]{0, 1, 2, 3}; myarraylist<integer> mylist = new myarraylist<integer>(arrays.aslist(testintarray)); } }

any ideas why doesn't work, , how go creating myarraylist?

you should alter constructor from:

public myarraylist(arraylist<comparable<t>> aslist)

to:

public myarraylist(list<t> aslist)

arrays.aslist doesn't homecoming java.util.arraylist, why new myarraylist<integer>(arrays.aslist(testintarray)) doesn't compile. arrays.aslist returns instance of nested class defined within arrays class, called arraylist, more importantly, homecoming type of arrays.aslist(t... a) list<t>.

java generics arraylist

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -