
public class text//自己取名。注意一致
{
public static void main (String args[ ])
{
int [] num1 = {1,2,3,4,5,6};
int [] num2 = {1,2,3,4};
int [] num3 = {};
int j=0;
for (int i=0;i<=num1length;i++)
{int a=num1[i]num2[i%4];
num3[j]=a;
j++;
}
}
int a[10];
声明int f();
int f()
{
return a;
}
数组名的指针,即数组首元素地址的指针。即是指向数组的指针。
例:int (p)[10]; p即为指向数组的指针,又称数组指针。
数组指针是指向数组地址的指针,其本质为指针;
指针数组是数组元素为指针的数组(例如 int p[3],定义了p[0],p[1],p[2]三个指针),其本质为数组。
数组指针的使用在某些情况下与指针数组很相似,要注意区别。
两种方案:
第一种:(推荐使用这种)
只要修改返回类型,并把
return tmplist改成return tmplisttotoArray();就行了 取出的是Object数组,需要强制转换成HashMap;
//返回值是数组
public Object[] quyListBySqlAndParams(String querystr, List params,int dno) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rst = null;
ResultSetMetaData rsmd = null;
List tmplist=new ArrayList();
try{
conn = myDataUtilgetConnection(dno);
pstmt = connprepareStatement(querystr);
//参数赋值
for(int i=0;i<paramssize();i++){
String param = paramsget(i)==null"":paramsget(i)toString()trim();
pstmtsetString(i+1,param);
}
rst = pstmtexecuteQuery();
rsmd = rstgetMetaData();
while (rstnext()) {
Map tmpmap = new HashMap();
for ( int i = 1; i <= rsmdgetColumnCount(); i++ ){
tmpmapput(rsmdgetColumnName(i), rstgetObject(i));
}
tmplistadd(tmpmap);
}
}catch(Exception e){
Systemoutprintln("pubgetstr"+etoString()+querystr);
}finally{
myDataUtilfree(rst,pstmt,conn);
//这里调用toArray()方法返回数组
return tmplisttoArray();
}
}
。
方案二:
原理就是将结果集的游标定位到最后一行,然后通过返回行号确定记录数,从而确定数组的长度。然后再将游标回退到beforefirst
但是一定要注意 这么做有个地方要考虑, 默认的ResultSet类型是TYPE_FORWARD_ONLY,只向前的游标,不允许指针回退;在定义时要将默认的ResultSet类型里的TYPE_FORWARD_ONLY改成ResultSetTYPE_SCROLL_INSENSITIVE,把你那句改成这个
pstmt = connprepareStatement(querystr, ResultSetTYPE_SCROLL_INSENSITIVE, ResultSetCONCUR_READ_ONLY);
//返回数组
public Object[] quyListBySqlAndParams(String querystr, List params,int dno) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rst = null;
ResultSetMetaData rsmd = null;
Object[] tmpAarry;
try{
conn = myDataUtilgetConnection(dno);
//注意 这里做了修改 pstmt=connprepareStatement(querystr,ResultSetTYPE_SCROLL_INSENSITIVE,ResultSetCONCUR_READ_ONLY);
//参数赋值
for(int i=0;i<paramssize();i++){
String param = paramsget(i)==null"":paramsget(i)toString()trim();
pstmtsetString(i+1,param);
}
rst = pstmtexecuteQuery();
rsmd = rstgetMetaData();
// 这里做了修改
rstlast();
int count = rstgetRow();
tmpAarry = new Object[count];
rstbeforeFirst();
while (rstnext()) {
Map tmpmap = new HashMap();
int j = 0;
for ( int i = 1; i <= rsmdgetColumnCount(); i++ ){
tmpmapput(rsmdgetColumnName(i), rstgetObject(i));
}
tmpAarry[j]=tmpmap;
j++;
}
}catch(Exception e){
Systemoutprintln("pubgetstr"+etoString()+querystr);
}finally{
myDataUtilfree(rst,pstmt,conn);
// 返回的是个数组
return tmpAarry;
}
}
好了,这两种方法的结构是一样的,看是你也看到了,自己写有多麻烦,还要考虑很多意想不到的问题,比如那个滚动结果集。所以能用java api中提供的最好用,那都是经得住考验的,而且方便的很。
你定义方法时定义的返回类型是int,但是你实际返回的Matrix是一个二维数组,当然报错了
这样改就可以了int[][] get_Matrix() {
Scanner in = new Scanner(Systemin);
Systemoutprintln("input the size of the Matrix");
int l = innextInt();
int m = innextInt();
Systemoutprintln("input elements of the Matrix");
int[][] Matrix = new int[l][m];
for (int i = 0; i < l; i++) {
for (int j = 0; j < m; j++) {
Matrix[i][j] = innextInt();
}
}
return Matrix;
}
就是将返回类型int改成int[][]就可以了
数组越界了,换成这个
int[][] ans = new int[2][2];关于接收,
由于java是值传递,那么你在调用了f方法后,可以直接 *** 作ans,你看看前后的变化
Systemoutprintln(ArraysdeepToString(ans));f(a, b, ans);
Systemoutprintln(ArraysdeepToString(ans));
如果你非要接收,也可以
Systemoutprintln(ArraysdeepToString(ans));ans = f(a, b, ans);
Systemoutprintln(ArraysdeepToString(ans));
两者效果是一样的
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)