怎么在一个方法里传参 但是参数的个数 和类型未知
即 使用的时候可能是2 个 3 个4 个...
可能是 int String Float
好像有个参数列表 怎么表示忘记了..
public static void command(Object o1...?? ){
code...
}
就是忘记参数哪里怎么写了 ...
即 使用的时候可能是2 个 3 个4 个...
可能是 int String Float
好像有个参数列表 怎么表示忘记了..
public static void command(Object o1...?? ){
code...
}
就是忘记参数哪里怎么写了 ...
class Box
{
double length;
double width;
double height; Box(double l,double w,double h)
{
length=l;
width=w;
height=h;
}
}public class BoxDemo{
public static void main(String args[]){ Box bw=new Box(1,2,3,5.6); //here
System.out.println("bw weight is:"+bw.weight);
}
}
参数类型不知,如果你知道可能有那些。可以 overload。同时不确定,去死吧。
void process(T... source);
}
class A{}
public class NewVarArgs {
static void printArray(Object...args){
for(Object obj:args)
System.out.print(obj+" ");
System.out.println();
}
public static void main(String[] args){
printArray(new Integer(47),new Float(3.14),new Double(11.11));
printArray(47,
printArray("one","two","three");
printArray(new A(),new A(),new A());
printArray((Object[])new Integer[]{1,2,3,4});
printArray();
}
}
// 操作
}我觉得,你也可以使用list啊~不过你需要转型,因为你的参数类型不确定。
public static void command(Object... args){
code...
}昨天刚有人发帖问到这个来着……
public abstract void sum(T... t);
}class IntSum implements ISum<Integer> {
public void sum(Integer... t) {
int s = 0;
for (int e : t) {
s += e;
}
System.out.println(s);
}
}class DoubleSum implements ISum<Double> {
public void sum(Double... t) {
double s = 0.0;
for (double e : t) {
s += e;
}
System.out.println(s);
}
}public class SumMain { public static void main(String[] args) { IntSum intSum = new IntSum();
intSum.sum(1, 2, 3, 4, 5);
intSum.sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); DoubleSum doubleSum = new DoubleSum();
doubleSum.sum(1.0, 1.5, 2.0, 2.5, 3.0);
doubleSum.sum(1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0); }
}
在连接到Oracle 数据库后要执行SQL 操作数据库
我想把所有的增.删.改.查 操作 用一个方法写.方法参数是sql语句和后面字段的条件
用preparedStatement预编译sql语句
在根据参数的个数 逐个赋值
执行ps.excute()方法 用getResult()取得结果集
但是遍历的时候不知道表有多少字段 和字段类型..不知道怎么写...
不知道这个想法 是对还是错.
以用户名、性别组合查询为例
用Map:Map paramMap = new HashMap();
paramMap.put("username", username);
paramMap.put("sex", sex);拼接SQL语句:
StringBuilder sql = new StringBuilder("select * from tuser where 1=1");
if(map.get("username")!=null && map.get("username").trim().length() > 3){
sql.append("username='" + map.get("username") + "'");
}
if(map.get("sex")!=null && map.get("sex").trim().length() > 0){
sql.append("sex='" + map.get("sex") + "'");
}
比如
String sql = "....where id=?and name = ?";int id = 5;String name = "jason";
我的方法是 public void execteSQL(id,name)...
若是sql没有条件 或者很多条件 在查询时怎么遍历结果集
我大概是想把这个方法作为一个模版 适用于所有增加 删除 修改和 查询的操作..
package sample;
class A{}
public class NewVarArgs {
static void printArray(Object...args){
for(Object obj:args)
System.out.print(obj+" ");
System.out.println();
}
public static void main(S……
[/Quote
+1]