Let me give some code so you can see what I'm doing with the following java code for android. Say for example I have the following two classes, one extended from the other:
class MyClassOne {
protected float x, y;
MyClassOne(float x, float y) {
this.x = x; this.y=y;
}
public void printY(){
System.out.print(y);
}
}
class MyClassTwo extends MyClassOne {
protected String stringSpecificToThisClass;
private long longSpecificTothisClass;
MyClassTwo(float x, float y, String s, long l) {
this.x=x; this.y=y;
this.longSpecificTothisClass= l; this.stringSpecificTothisClass=s;
}
}
These classes are then initialized in the following way
private ArrayList<MyClassOne> mClassOne = new ArrayList<MyClassOne>();
private ArrayList<MyClassTwo> mClassTwo = new ArrayList<MyClassTwo>();
for(int i=0;i<5;i++){
Random random = new Random(10);
mClassOne.add(new MyClassOne(i*12, random.nextInt()));
mClassTwo.add(new MyClassOne(i*11, random.nextInt()));
}
now, what I want to do is compare and sort both arraylists according to the value of y.
The way i do this for a single list is like so:
private Object[][] mSort(){
Object[][] mSort = new Object[mClassOne.size()][2];
for(int i = 0; i<mClassOne.size(); i++){
mSort[i][0] = i;
mSort[i][1] = mClassOne.get(i).y;
}
Arrays.sort(mSort, new Comparator<Object[]>(){
#Override
public int compare(Object[] obj1, Object[] obj2){
Float comp1 = (Float)obj1[1]; Float comp2 = (Float) obj2[1];
return comp1.compareTo(comp2);
}
});
return mSort;
}
Object[][] mSort = mSort();
for(int i=0;i<mClassOne.size();i++){
int z = (Integer)mSort[i][0];
mClassOne.get(z).printY();
}
which could output something like this:
2, 4, 5, 6, 9
Hopefully the code above is clear enough so others can see what I'm trying to do; the question is:
"How could I combine both ArrayLists then sort them by their respective y value."
The Answer I was looking for
ArrayList<MyClassOne> mTest = new ArrayList<MyClassOne>();
mTest.addAll(mClassOne);
mTest.addAll(mClassTwo);
Collections.sort(mTest, new Comparator<MyClassOne>(){
#Override
public int compare(MyClassOne obj1, MyClassTwo obj2) {
return (int) (obj1.getY() - obj2.getY()); }
}
);
// mTest is now sorted, verified by ~ foreach(mTest) {print mTest.getY(); }
Well, you could use a Comparator<MyClassOne> which should be able to handle MyClassTwo instances as well, since MyClassTwo extends MyClassOne.
Just create a single List<MyClassOne>, add all elements of the other lists and sort.
In cases where the classes don't extend each other, introduce a common interface.
Related
While coding I was trying to declare a class that can create an arraylist of arraylists, but soon enough I found it hard to define a proper constructor for my class. I wanted to define some methods for me to handle the huge outer arraylist(1000*1000), but I might be affected by C and always tried to use something like structdef.
How should I define my class? I guess declaring every lines seperatedly is not a wise choice, and I don't want to use 2D arraylist directly. Besides, how should I define a constructor to get an object that is an 2D arraylist?
//Update here
Below is my code example:
class farbicMap {
//attribute
ArrayList<Integer> farbicUnit = new ArrayList<Integer>();
//constructor
farbicMap () {
for (int i=0;i<1000;++i) {
farbicUnit.add(0);
}//this gives an arraylist with size of 100
//I want to use the above arraylist to construct another list here
}
//method
setUnitValue(int v) {
...
}
}
Seems that I didn't really understand the concept of class... I wanted to use the class to represent a map with some nodes. Now that's much clearer to me.
This is how I understood your consern:
class Test {
public static void main(String[] args) {
Board board = new Board(1000, 1000);
board.put(1, 2, "X");
Object x = board.get(1, 2);
System.out.println("x = " + x);
}
}
class Board {
private final int xSize;
private final int ySize;
private ArrayList<ArrayList<Object>> board = new ArrayList<>();
public Board(int xSize, int ySize) {
this.xSize = xSize;
this.ySize = ySize;
for (int i = 0; i < xSize; i++) {
board.add(getListOfNulls());
}
}
public Object get(int x, int y) {
return board.get(x).get(y);
}
public void put(int x, int y, Object toAdd) {
List<Object> xs = board.get(x);
if (xs == null) {
xs = getListOfNulls();
}
xs.add(y, toAdd);
}
private ArrayList<Object> getListOfNulls() {
ArrayList<Object> ys = new ArrayList<>();
for (int j = 0; j < ySize; j++) {
ys.add(null);
}
return ys;
}
}
You should use Array if size is fixed.
I wanna change a variable's name by choice of user. I know that it can be done by Mab (not very charming), but I think that it can be done by polymorphism too, at least something that can simulate it. It is hard to explain, so the code below can illustrate it better. Thanks!
public class Main {
public static void main(String[] args) {
GenericObject o;
o = new Object1(10, 10);
o.wh();
System.out.println(o.w); // Output: 3 (ok)
System.out.println(o.h); // Output: 10 (ok)
o = new Object2(10, 10);
o.wh();
System.out.println(o.w); // Output: 7 (ok)
System.out.println(o.h); // Output: 4 (ok)
String inputFromUser = "1";
o = new Object + inputFromUser + (10, 10); /*I know that is an absurd, just to illustrate...
if polymorphism can solve this problem, I thik it's the best option. So how use it here?
I don't wanna use ifs or switchs, I will use more than 300 classes*/
o.wh();
System.out.println(o.w); // Output: 3 (that's what I wanna obtain)
System.out.println(o.h); // Output: 10 (that's what I wanna obtain)
}
}
abstract class GenericObject {
int w, h, x, y;
GenericObject (int x, int y) {
this.x = x;
this.y = y;
}
public abstract void wh();
}
class Object1 extends GenericObject{
Object1 (int x, int y) {
super(x, y);
}
#Override
public void wh () {
w = 3;
h = 10;
}
}
class Object2 extends GenericObject{
Object2 (int x, int y) {
super(x, y);
}
#Override
public void wh () {
w = 7;
h = 4;
}
}
Hope below code will help you.
HashMap<String, GenericObject > allTypes = new HashMap<>();
allTypes.put("1", new Object1(10, 10));
allTypes.put("2", new Object2(10, 10));
String inputFromUser = "1";
GenericObject o = allTypes.get(inputFromUser);
o.wh();
System.out.println(o.w); // Output: 3
System.out.println(o.h); // Output: 10
All design smells aside, I think that you're really looking for an answer using reflection. I think you can use the following code snippet to accomplish what you want:
// dynamically specify the class name based on the user's input.
Class<?> typeFromUser = Class.forName("Object" + inputFromUser);
// grab the correct package-private or public constructor whose type
// parameters are 2 primitive ints
java.lang.reflect.Constructor<?> constructor =
typeFromUser.getDeclaredConstructor(Integer.TYPE, Integer.TYPE);
// reflectively invoke the constructor with the two int values
Object obj = constructor.newInstance(10, 10);
// the instantiated object is of a raw type, cast it to the correct type
o = (GenericObject) obj;
I also tried this code and you can see the demo on IDEOne.
I think you can use reflection mechanism to do this. Below is the example code snippet:
String clsName = <your input(Object1 or Object2)>;
Class<?> clazz = Class.forName(clsName);
Constructor<?> cst = clazz.getDeclaredConstructor(int.class, int.class);
GenericObject obj = null;
if(clsName.equals("Object1")) {
obj = (Object1)cst.newInstance(10, 10);
} else if(clsName.equals("Object2")) {
obj = clsName.equals("Object1")
}
How can I program my class to accept both Integers and Floats, I suppose I'll need to use generics, am I correct?
public class Vec2 {
private int x, y;
public Vec2(int xa, int ya) {
this.x = xa;
this.y = ya;
}
public Vec2() {
this(0, 0);
}
public Vec2(Vec2 vec) {
this(vec.x, vec.y);
}
public void addX(int xa) {
x+=xa; // I get an exception here when I try to use generics.
}
public void addY(int ya) {
y+=ya; // I get an exception here when I try to use generics.
}
Any ideas how to program my class to accept floats, integers and doubles altogether?
For the time being, we cannot have generics over primitives like int or double, so you will be forced to use boxed representations. It really is easier to just make a separate class for int and double. But if you want to use generics, here's how you can do it in a type-safe way (using java8):
public class Vec2<T> {
private final BinaryOperator<T> adder;
private T x, y;
private Vec2(BinaryOperator<T> adder, T x, T y) {
this.adder = adder;
this.x = x;
this.y = y;
}
public void addX(T xa) {
x = adder.apply(x, xa);
}
public void addY(T ya) {
y = adder.apply(y, ya);
}
public static Vec2<Integer> ofInt(Integer x, Integer y) {
return new Vec2<>(Integer::sum, x, y);
}
public static Vec2<Double> ofDouble(Double x, Double y) {
return new Vec2<>(Double::sum, x, y);
}
}
Vec2<Integer> intvec = Vec2.ofInt(5, 3);
intvec.addX(8);
Vec2<Double> dblvec = Vec2.ofDouble(5.2, 8.9);
dblvec.addY(-.9);
You could use BigDecimal to back your Vec2 and then you could use create addX and addY method(s) for long and double fairly easily. Something like,
public class Vec2 {
private BigDecimal x, y;
public Vec2(double xa, double ya) {
this.x = BigDecimal.valueOf(xa);
this.y = BigDecimal.valueOf(ya);
}
public Vec2(long xa, long ya) {
this.x = BigDecimal.valueOf(xa);
this.y = BigDecimal.valueOf(ya);
}
public Vec2(Vec2 vec) {
this.x = vec.x;
this.y = vec.y;
}
public void addX(double xa) {
x = x.add(BigDecimal.valueOf(xa));
}
public void addX(long xa) {
x = x.add(BigDecimal.valueOf(xa));
}
public void addY(double ya) {
y = y.add(BigDecimal.valueOf(ya));
}
public void addY(long ya) {
y = y.add(BigDecimal.valueOf(ya));
}
#Override
public String toString() {
return String.format("x = %s, y = %s", x.toString(), y.toString());
}
}
No, you don't, merely have your class inherit from Number and use type checking to ensure that values are of the appropriate class, if necessary, e.g.
Class IsraelG99sClass {
Number n;
public Number add(Number n2) {
if (n instanceof Integer && n2 instanceof Integer) {
return new Integer(n.intValue() + n2.intValue());
} else {
return new Double(n.doubleValue() + n2.doubleValue());
}
}
public Number getValue() {
if ((n instanceof Integer) || (n instanceof Float)) {
return n;
} // handle the other case as appropriate
}
}
Floats and ints are very different values with vastly different mins and maxes. I would try using doubles as the data member with overloaded constructors for different variable types instead of generics unless generics are really needed.
Yes, you can use generics and make your x and y attributes of type T.
But you won't be able to just implement the addX and addY in they way you want.
Check these other answers on how to implement a generic number addition, it's not as simple but you should be able to do it that way.
Java Generics and adding numbers together
how to write a generic method for adding numbers
First of all, I'm operating under the assumption that you want x and y to be of varying (generic) type.
For this, you would want:
public class Vec2<E extends Number> {
private E x, y;
public Vec2(E xa, E ya) {
this.x = xa;
this.y = ya;
}
//Not _easily_ possible with generics, as the compiler has no guarantee that
//zero is an acceptable value. Consider some variation of a Factory pattern,
//but this will work. Note that there is an "officially"-unchecked cast warning.
public Vec2() {
super();
final Number zero = 0.0;
this.x = (E)zero;
this.y = (E)zero;
}
public Vec2(Vec2<E> vec) {
this(vec.x, vec.y);
}
public void addX(E xa) {
Number c = x.doubleValue() + xa.doubleValue();
x = (E)c;
}
public void addY(E ya) {
Number c = y.doubleValue() + ya.doubleValue();
x = (E)c;
}
This should work well. While I encourage you to use generics, note that keeping a numeric type (like int, float, or double) as a generic is often not advisable, as they're only similar on the surface. When you dig into the operations of, say, "+", they are radically different dependent on type. You will also have an assortment of unchecked-cast warnings in this code; perhaps I could have rooted them out properly had I the time, but this just goes back to my warning about generic numbers.
You will also notice a few flukes of the language doing this, such as the way that (E)zero works, but (E)(0.0) does not.
By and large, though, generics are a much easier and cleaner way to go about things than inheritance, when it is possible.
I would like to create the method .add(int,int) for and ArraYList of the following data structure:
class CPoint {
int x;
int y;
}
in order to use it in the following way:
ArrayList<CPoint> ar = new ArrayList<CPoint>();
ar.add(100,100);
I know I have to start with something like this:
class CArrayListPoints extends ArrayList<CPoint>{
}
but not sure how. Please help me in this matter.
EDIT: implement <-- extends
You can create a class like this:
class CArrayListPoints extends ArrayList<CPoint>{
public void add(int x, int y){
super.add(new CPoint(x, y);
}
}
Use this way:
CArrayListPoints list = new CArrayListPoint();
list.add(10, 10);
Probably You are looking something like this
class CPoint {
int x;
int y;
}
class CArrayListPoints extends ArrayList<CPoint>{
public void add(int x, int y){
CPoint p = new CPoint();
p.x = x;
p.y = y;
add(p);
}
}
If you want a list with two values HashMap http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html is what you need. If you insist on using Arraylist, just make a add(int a,int b)method and have each CPoint have a field that contains the x value.
I am trying to return two numbers from this method. I thought this was correct. Where am I going wrong?
public int[] getDimension() {
int shapeWidth = 0;
int shapeHeight = 0;
// .....
int[] result = new int[] {shapeWidth, shapeHeight};
return result;
}
And then at a calling site, is this correct?
public int getWidth() {
return getDimension()[0];
}
I am asking because I believe there's a bug but I don't see it.
That's fine. Short but complete program to demonstrate it working:
public class Test {
public static void main(String args[]) {
int width = getDimension()[0];
System.out.println(width);
}
public static int[] getDimension() {
int shapeWidth = 5;
int shapeHeight = 10;
int[] result = new int[] {shapeWidth, shapeHeight};
return result;
}
}
You can make the result declaration line slightly simpler, by the way:
int[] result = {shapeWidth, shapeHeight};
Rather than using an array, I would recommend using a class
class Dimensions {
private int width;
private int height;
public Dimensions(int width, int height) {
this.width = width;
this.height = height;
}
// add either setters and getters
// or better yet, functionality methods instead
}
This will give you compile time referential integrity, which is much better than inferring based on "we know index 0 is width and index 1 is height".
If you still want to use an array, Jon's answer is spot on.
Your code looks fine, but try not to use an array if you only need a pair.
Since Java doesn't have tuples/pairs you have to implement them, but it's pretty easy. Refer to this question for a possible implementation.
public class Test {
public static void main(String args[]) {
int width = getDimension().getLeft();
System.out.println(width);
}
public static Pair<Integer, Integer> getDimension() {
int shapeWidth = 5;
int shapeHeight = 10;
return new Pair<Integer, Integer>(shapeWidth, shapeHeight);
}
}
This is better than a Dimension class, because you can use it everywhere in your code.