怎么在一个方法里传参 但是参数的个数 和类型未知 
       即 使用的时候可能是2 个 3  个4 个...
                      可能是 int String Float
   好像有个参数列表 怎么表示忘记了..
    public static void command(Object o1...?? ){
          code...
    }
   就是忘记参数哪里怎么写了 ...

解决方案 »

  1.   

    For example:
    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);
        }
    }
      

  2.   

    参数个数不定可以使用可变参数。
    参数类型不知,如果你知道可能有那些。可以 overload。同时不确定,去死吧。
      

  3.   

    public interface Processor<T> {
        void process(T... source);
    }
      

  4.   

    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(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();
    }
    }
      

  5.   

    public void trans(Object... object) {
    // 操作
    }我觉得,你也可以使用list啊~不过你需要转型,因为你的参数类型不确定。
      

  6.   


    public static void command(Object... args){
      code...
    }昨天刚有人发帖问到这个来着……
      

  7.   

    interface ISum<T> {
    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); }
    }
      

  8.   

    不确定参数的个数,可以使用“可变参数列表”,《Java编程思想 第四版》 5.8.1 里有详细讲不确定参数的类型,可以使用泛型,这个百度上搜都可以。要例子的话,3楼已经给出了。ps:一楼给的是神马???编译都通不过的就放上来
      

  9.   

    你可以尝试传递一个ArrayList,在动态数组里随便装参数。
      

  10.   

    java里面我记得确实支持可变参数,但是安全方面不好控制。最好还是放到一个list里面传进来。
      

  11.   

    我的想法是 :
       在连接到Oracle 数据库后要执行SQL 操作数据库 
       我想把所有的增.删.改.查 操作 用一个方法写.方法参数是sql语句和后面字段的条件
         用preparedStatement预编译sql语句
         在根据参数的个数 逐个赋值
         执行ps.excute()方法  用getResult()取得结果集
          但是遍历的时候不知道表有多少字段 和字段类型..不知道怎么写...
     
       不知道这个想法 是对还是错.     
      

  12.   

    可以用Map或者Bean来保存查询条件~
    以用户名、性别组合查询为例
    用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") + "'");
    }
      

  13.   

    若是我不知道表都有哪些字段 ,在SQL中我甚至不知道 有没有条件 有多少个条件 条件的类型..
        比如
          String sql = "....where id=?and name = ?";int id = 5;String name = "jason";
                我的方法是 public void execteSQL(id,name)...
            若是sql没有条件 或者很多条件  在查询时怎么遍历结果集 
       我大概是想把这个方法作为一个模版 适用于所有增加 删除 修改和 查询的操作..
      

  14.   

    [Quote=引用 4 楼 dyllove98 的回复:]Java code
    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]