| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2620 人关注过本帖
标题:java关于传任意类型参数的问题
取消只看楼主 加入收藏
小文文化
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2017-9-5
结帖率:100%
收藏
 问题点数:0 回复次数:0 
java关于传任意类型参数的问题
我有以下一个java程序:
package javatest1;

import java.util.ArrayList;
import java.util.List;

public class javatest1 extends common{

    /**
     * @param args
     */   
    public static void main(String[] args) {
        // TODO Auto-generated method stub
             /* String c="Hello World!";  
              String d="您好!";
              String e="0123456789";
              stringchangeasc(c);
              printseparator();
              stringchangeasc(d);
              printseparator();
              stringchangeasc(e);
              printseparator(); */              
              /*String c="Hello World! Welcome China![520]";
              print("原字符串是:"+c);
              printseparator();
              StringBuilder resultstring=stringchangcw(c);
              StringBuilder resultstring2=stringchangeasc(c);;
              print("在原字符的基础中,每一个字符串大写改为小写,小写改为大写,不是字母不改变:"+resultstring);
              printseparator();
              print("原字符串每一个字符的ascii码:");
              print(resultstring2);
              printseparator();*/
              /*int cai = 0;
              char[] ca;              
              List cb=new ArrayList();            
              StringBuilder sb=new StringBuilder();
              ca = c.toCharArray();              
              for(int i=0;i<ca.length;++i)
                 {
                  if(ca[i]>65 && ca[i]<90)
                  {
                      cai=ca[i]+32;
                  }
                  else
                      if(ca[i]>97 && ca[i]<122)
                      {
                          cai=ca[i]-32;
                      }
                      else
                      {
                          cai=ca[i];
                      }
                     //cai=ca[i]+32;   
                     cb.add(cai);               
                    }         
              for(int j=0;j<cb.size();++j)
                 {
                      int obji=(int) cb.get(j);                        
                      char oi=(char)obji;
                      sb.append(oi);                  
                    }   
              print(sb);    */   
              //int result1=argadd(5);
              //int result2=argadd(5,4);
              //int result3=argadd(1,2,3);
              //int result4=argadd(5,7,8,2,3,1,4,6,9,10);
              //int result5=argmax(5,7,8,2,3,1,4,10,6,9,15);
             // int result6=argmin(5,7,8,2,3,1,4,10,6,9,15);        
              int[] result8=argascsort(5,7,8,2,3,1,4,10,6,9,15);
              int[] result9=argdescsort(5,7,8,2,3,1,4,10,6,9,15);
            //int[] result8=argascsort(5,4,3);
             //int[] result8=argascsort(8,7,5);
             //print(8);
            // print(7);
            // print(5);
             // print(result1);
             // print(result2);
             // print(result3);
             // print(result4);
              int len=result8.length;
              for(int i=0;i<len;++i)
              {
                  print("第"+(i+1)+"个数据是"+result8[i]);
              }
              printseparator();
              int len2=result9.length;
              for(int i=0;i<len2;++i)
              {
                  print("第"+(i+1)+"个数据是"+result9[i]);
              }         
              int a=1;
              int b=2;
              swapobj so1=new swapobj();
              swapobj so2=new swapobj();
              so1.swapvalue=1;
              so2.swapvalue=2;
              sortdata(so1,so2);
              print(so1.swapvalue);
              print(so2.swapvalue);
    }
   
   
            
}
以下是common类文件:
package javatest1;

import java.util.ArrayList;


import java.util.List;


class swapobj<E>
{
    E swapvalue;
}

public class common {
   
    //简单打印
    public static <E> void print(E value)
    {
        System.out.println(value);
    }

    //字符串每个字符变成ascii码
    public static StringBuilder stringchangeasc(String value)
    {
         char[] ca;
         int cai;
         StringBuilder sb=new StringBuilder();
         ca = value.toCharArray(); //把传过来的value转换为字符数组           
         for(int i=0;i<ca.length;++i)
         {
             cai=ca[i];
             //print(ca[i]+"的ascii码是"+cai);
             sb.append(ca[i]+"的ascii码是"+cai);
             sb.append("\n");
         }
         return sb;
    }
   
    //如果输出内容是不同部分的话,用************************************隔开   
    public static void printseparator()
    {
        print("************************************");
    }
   
    //字符串中每一个字符串大写变小写,小写变大与,如果不是字母的话不改变
    public static StringBuilder stringchangcw(String value)
      {
          int cai = 0;
          char[] ca;              
          List cb=new ArrayList();            
          StringBuilder sb=new StringBuilder();
          ca = value.toCharArray();              
          for(int i=0;i<ca.length;++i)
             {
              if(ca[i]>65 && ca[i]<90)
              {
                  cai=ca[i]+32;
              }
              else
                  if(ca[i]>97 && ca[i]<122)
                  {
                      cai=ca[i]-32;
                  }
                  else
                  {
                      cai=ca[i];
                  }
                 //cai=ca[i]+32;   
                 cb.add(cai);               
                }         
          for(int j=0;j<cb.size();++j)
             {
                  int obji=(int) cb.get(j);                        
                  char oi=(char)obji;
                  sb.append(oi);                  
                }   
          return sb;              
    }
   
     //对传过来的值相加,得到传过来的值的和,传过来的参数个数不限,喜欢多少个参数都可以
     public static int argadd(int... arg){
           int sum = 0,len=arg.length;           
           for(int i=0;i<len;++i){  
               sum=sum+arg[i];  
            }
           return sum;
        }
   
     //对传过来的值找出最大值,传过来的参数个数不限,喜欢多少个参数都可以
     /*public static int argmax(int... arg){
           int max = arg[0],len=arg.length;
           for(int i=1;i<len;++i){  
               if(max<arg[i])
               {
                   max=arg[i];
               }
            }
           return max;
        }*/
   
     public static <E> E argmax(E... arg){
           E max = arg[0];
           int len=arg.length;
           for(int i=1;i<len;++i){  
               if(max<arg[i])
               {
                   max=arg[i];
               }
            }
           return max;
        }
   
     //对传过来的值找出最小值,传过来的参数个数不限,喜欢多少个参数都可以
     public static int argmin(int... arg){
           int min = arg[0],len=arg.length;
           for(int i=1;i<len;++i){  
               if(min>arg[i])
               {
                   min=arg[i];
               }
            }
           return min;
        }
   
     //对传过来的值进行由小到大排序,传过来的参数个数不限,喜欢多少个参数都可以
     public static int[] argascsort(int... arg){
           int index,temp,len=arg.length;   
           //print(len);
          // print(min);
           for(int i=0;i<len-1;++i){   
               index=i;
               for(int j=i+1;j<len;++j)
               {
               if(arg[index]>arg[j])
               {
                   index=j;
               }              
               }               
               temp=arg[i];
               arg[i]=arg[index];
               arg[index]=temp;   
            }
            return arg;
        }
   
     //对传过来的值进行由大到小排序,传过来的参数个数不限,喜欢多少个参数都可以
     public static int[] argdescsort(int... arg){
           swapobj so1=new swapobj();
           swapobj so2=new swapobj();
           int index,temp,len=arg.length;         
           for(int i=0;i<len-1;++i){   
               index=i;
               for(int j=i+1;j<len;++j)
               {
               if(arg[index]<arg[j])
               {
                   index=j;
               }              
               }               
               temp=arg[i];
               arg[i]=arg[index];
               arg[index]=temp;                        
             //so1.swapvalue=arg[i];
            // so2.swapvalue=arg[index];
            // sortdata(so1,so2);
            }
            return arg;
        }
   
     //交换传过来的value1和value2的值
     public static void sortdata(swapobj value1,swapobj value2)
     {
          swapobj temp = new swapobj();
          temp.swapvalue=value1.swapvalue;
          value1.swapvalue=value2.swapvalue;
          value2.swapvalue=temp.swapvalue;         
     }
   

}
但运行程序时出现以下错误:
The operator < is undefined for the argument type(s) E, E
各位高手们,我想argmax这个函数的参数类型不只是int类型,传String,Char或者int都可以执行,但只是编写一个函数,不想编多个重载函数

搜索更多相关主题的帖子: public arg print int for 
2017-09-07 15:51
快速回复:java关于传任意类型参数的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.042151 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved