在线揭帖:使用类封装操作后就执行出问题了

h3w4 2007-01-30 10:10:25
///////////////////////////////////sort.java
import java.io.*;
public class sort
{
public static void main(String args[]) throws IOException
{
int myArray[]={99,88,77,66,55,44,33,22,11};
sortTest st = new sortTest ( myArray );
st.init(); //初始化写文件的那些对象
// logic operation
st.out_file ( "Before Sorting:" ); // 没有写入文件
st.out_crt ("before"); // 执行正确
st.sortnub (); // 进行排序
st.out_crt ("after"); // 执行正确
st.out_file ( "After Sorting:" ); // 没有写入文件
}
}
///////////////////////////////sortTest.java
import java.io.*;
class sortTest
{
// data member
private int myArray[];
private FileWriter fw;
private BufferedWriter bf;
private PrintWriter out;
// member function
sortTest ( int arr[] ) throws IOException
{
myArray = arr;
}
protected void finalize() throws IOException
{
out.close();
}
void init() throws IOException
{
fw = new FileWriter("hh");
bf = new BufferedWriter(fw);
out = new PrintWriter(bf);
}
void sortnub()
{
for( int i=0;i<myArray.length;i++){
for( int j=i+1;j<myArray.length;j++){
if ( myArray[i]>myArray[j]){
int temp=myArray[i];
myArray[i]=myArray[j];
myArray[j]=temp;
}
}
}
};
void out_crt ( String s )
{
System.out.println(s);
for (int i=0;i<myArray.length;i++)
System.out.print(myArray[i]+" ");
System.out.println();
};
void out_file ( String text ) throws IOException
{
out.print(text+"\n");
for (int i=0;i<myArray.length;i++)
out.print(myArray[i]);
out.print("\n");
};
};

%java sort
before
99 88 77 66 55 44 33 22 11
after
11 22 33 44 55 66 77 88 99
% ls -l
-rwxr-xr-x 1 root wheel 0 Jan 30 09:53 hh

但是一开始sortTest.init的内容是些在sort.main中的,然后传参进去,执行都好着呢,不知改后为什么会这样
由于第一次写java,请大家不吝赐教,此外如果觉得程序的写法,分布...等任何地方有问题的请提出来,谢谢哦
...全文
259 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
believefym 2007-01-30
  • 打赏
  • 举报
回复
楼主认为会自动调用finalize()?
china8848 2007-01-30
  • 打赏
  • 举报
回复
try
{
可能出现异常的语句;
}
catch(Exception e)
{System.out.println(e.getStackTrace());}
我是初学者,不对的地方请谅解。
「已注销」 2007-01-30
  • 打赏
  • 举报
回复
main()方法后面添加代码
st=null;
System.runFinalization();

问题的关键还是在于没有关闭文件.由于该实例的finalize方法没执行
(没有调用runFinalization或垃圾回收),正确关闭文件之后就可以正确写入文件了
h3w4 2007-01-30
  • 打赏
  • 举报
回复
哦,去掉分号(空语句,但不会报错)
如果throws IOException不是好方法,那么怎样写才更合适呢?
china8848 2007-01-30
  • 打赏
  • 举报
回复
哦,没想明白是为什么,另外方法{}后面的分号是不是应该去掉啊,还有throws IOException不是太好的做法吧。
h3w4 2007-01-30
  • 打赏
  • 举报
回复
to china8848(永远在一起):
一开始我就是这样做的,也确实可以写如文件
但经过封装后就不行了,代码如题
china8848 2007-01-30
  • 打赏
  • 举报
回复
import java.io.*;
public class sort
{

private static FileWriter fw;
private static BufferedWriter bf;
private static PrintWriter out;
public static void main(String args[]) throws IOException
{
int myArray[]={99,88,77,66,55,44,33,22,11};
fw = new FileWriter("hh.txt");
bf = new BufferedWriter(fw);
out = new PrintWriter(bf);
for (int i=0;i<myArray.length;i++)
out.print(myArray[i]);
out.close();

}
就能写入文件了
believefym 2007-01-30
  • 打赏
  • 举报
回复
楼主什么问题?
还有,}后面加那么多分号干吗
china8848 2007-01-30
  • 打赏
  • 举报
回复
protected void finalize() throws IOException
{
out.close();
}
关了啊
journay 2007-01-30
  • 打赏
  • 举报
回复
别忘了关闭文件操作
内容概要:本文围绕不确定环境下的多式联运路径优化问题展开研究,提并实现了基于AFO算法、遗传算法(GA)和粒子群优化算法(PSO)的三种智能优化方法,并借助Matlab平台完成算法编程与仿真。研究构建了考虑时间、成本、转运风险等多重不确定因素的路径优化模型,系统比较了AFO、GA、PSO三种算法在收敛速度、全局寻优能力和稳定性方面的表现,同时引入Matlab自带的全局优化搜索器作为基准对照,深入分析各算法在复杂物流网络中的适用边界与性能差异。研究表明,AFO算法在解决此组合优化问题时展现更快的收敛效率和更强的局部规避能力。; 适合人群:具备一定Matlab编程基础与运筹优化知识,从事物流工程、交通运输规划、智能算法开发等相关领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于多式联运、综合货运网络中的路径决策支持系统构建;②为不确定性条件下复杂路径规划问题提供智能算法选型依据与技术实现方案;③支持科研人员复现主流优化算法并开展横向性能对比实验,推动算法改进与实际落地。; 阅读建议:建议读者结合提供的Matlab代码逐模块分析算法实现流程,重点理解目标函数设计、约束条件处理及参数敏感性分析部分,可通过调整问题规模与算法参数进行对比实验,进一步拓展至动态路径规划或大规模网络优化等延伸场景。

62,622

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧