请教一个笔试题和一个基础问题:)
题( ): 1. 一个文本文件有多行,每行为一个URL。请编写代码,统计出URL中的文件名及出现次数。
a)文件名不包括域名、路径和URL参数,例如http://www.rs.com/n.op/q/rs?id=1中的文件名是rs。
b)部分URL可能没有文件名,例如http://www.abc.com/,这类统计为“空文件名”。
c)出现在不同URL中的相同文件名视为同一文件名,例如http://www.ceshi.com/hi.php
和ftp://ftp.cdef.com/hi.php为同一文件名
文件内容示例如下:
http://www.test.com/abc/de/fg.php?id=1&url=http://www.test.com/index.html
http://www.ceshi.com/hi.jsp
ftp://ftp.ceshi.com/hi.jsp
http://www.hello.com/cw/hi.jsp?k=8
http://www.hi.com/jk/l.html?id=1&s=a.html
http://www.rs.com/n.op/q/rs?id=1
http://www.abc.com/
/**以下是我写的方法因为确实太累赘但想不出别的方法,如果您有好的方法请指教指教~谢谢 **/
// 读入题目所给出的字符
import java.io.*;
public class FileStream{
private String str= " ";
String result= " ";
public void returnStr (){
Fenjie fj=new Fenjie();//分解处理字符串的类
File f = new File( "e:\\hello.txt "); //文件内容就是题目所给的7行URL地址
try
{
BufferedReader br=new BufferedReader( new FileReader(f) );
while((result=br.readLine())!=null ){
fj.aA(result);//在这里处理字符串
}
fj.display(); //打印处理结果
}
catch (Exception e){System.out.println(e.getMessage());}
}
} // End FileStream class
class Fenjie
{
String str= " ";
int d=0,h=0,count=0;
String newstr= " ";
String []string= new String[20];
public void aA(String s){ //提取每一行最后一个 "/ "与 ". "之间的字符串
str=s;
String []x = new String[str.length()];
h = str.lastIndexOf( "/ ");
d = str.lastIndexOf( ". ");
if (h> d) // 个别以 "? "结尾的文件名,如 " rs?id=1 "
d = str.lastIndexOf( "? ");
if ( h > d) // 如果是空文件名
d=h+1;
newstr= str.substring(h+1,d);
int ii=newstr.lastIndexOf( ". ");
if( ii > 0 )
newstr=newstr.substring(0,ii);
string[count]=newstr;
if(string[count].equals( " ")) // 判别空文件名
string[count]= "空文件名 ";
count++;
}
// 声明新的数组,打印不是 "重复出现 "的数组.
public void display(){
int [] w = new int [count];
for(int i=0;i <count;i++)
w[i]=1;
for(int i=0 ; i < count ; i++ )
for(int k=i+1;k <count-1;k++)
if( string[i].equals(string[k]) ){
string[k]= "重复出现 ";
++w[i];
}
for(int i=0 ;i <count;i++ )
if(string[i] != "重复出现 ")
System.out.printf( "%-10s 出现了 %10d 次\n ",string[i],w[i]);
}
public void delete(int x){
for(int i =x; i <count ;i++)
string[x]=string[x+1];
}
} //End Fenjie class
class Jie {
public static void main(String[] args) {
new FileStream().returnStr();
}
}
/*输出结果:
index 出现了 1次
hi 出现了 3次
l 出现了 1次
rs 出现了 1次
空文件名 出现了 1次
*/
/*********************************************************/
另外一个基础问题,我在CSDN看到如下基础要点:
18、当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?
是值传递。首先明确一个重要概念:Java不像C++支持两种参数传递方式(传值、传址),Java只支持参数传
值的方式。当一个对象实例作为一个参数被传递到调用的方法中时,方法的参数实际上是对该对象的引用的值的
副本。对象的内容可以借这个引用副本在被调用的方法中被改变,但这个引用副本本身的改变是不会影响到方法
外的,更不可能更改对象(因为引用副本改变了就不再指向该对象)。所以这里说的“并可返回变化后的结果”并
不准确,变化是因为被调用方法里使用获得的对象引用地址直接更改了对象,而不是“返回”。所以像C/C++里
著名的swap(int, int)方法,Java是没有办法像那样实现的,但这并不影响Java的使用。
//数组不就是按地址传递吗.还是我看漏了或不理解? 求答,谢谢
class One{
void kK(String []a){
a[0]= "C... ";
System.out.println( "kK() "+a[0]+ " "+a[1]);
}
String getStr(String []b){
b[1]= "Double ";
System.out.println( "getStr() "+b[0]+ " "+b[1]);
return b[1];
}
}
class Two {
void two(){
String []x = { "B.method ", "Bb.method ", " "};
One one = new One();
String []k=new String[1];
one.kK(x);
x[2]=one.getStr( x );
System.out.printf( "two() %s %s ",x[0],x[1] );
}
}
public class A {
public static void main(String args[]){
new Two().two();
}
}
/*输出结果
kK() C... Bb.method
getStr() C... Double
two() C... Double
*/
[解决办法]
java.lang.StringBuilder
[解决办法]
BufferedReader.readLine()//读取一行
String.lastIndexof( "/ ")+String.substring()//取得最后一个/后面的子串
分析获得的子串,保存到容器如HashSet,避免重复统计
细节问题楼主自己再想想吧