今天面试的上机题,帮忙看看

pxl_1012 2013-05-28 07:41:19


//题的大概结构就这样。哪位高手能实现啊。。
public class A {
/**
* 用来初始化电影
* 存储结构:电影ID 电影名字 演员(多个)
* @param s
*/
public A(String[] s ){

}
// 根据电影id 查找电影名
public String getNane(int id){

return null;
}
// 根据演员名查找电影名。演员要全在电影里面
public int[] getAll (String[] s){//String[] 记不清楚是数组还是字符串了。
return null;

}
}

...全文
229 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wkcing 2013-05-29
  • 打赏
  • 举报
回复

HashMap<String, String[]> MovieMap = new HashMap<String, String[]>();
MovieMap.put("","");
Set<MovieMap.Entry<String, String[]>> ens = MovieMap.entrySet();
		for(MovieMap.Entry<String, Student> en : ens)
		{
			System.out.println(en.getKey() + "-->" +  en.getValue());
		}

冰河1258888 2013-05-29
  • 打赏
  • 举报
回复
上面的有点问题 ,修改了一下 。下面代码测试OK了


package test;

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

public class Test2 {
	
	private String[] s;
	
	
	public Test2(String[] s){
		this.s = s;
	}
	
	public String getName(int id){
		for(String str:s){
			String[] ss = str.split(" ");
			if(Integer.parseInt(ss[0])==id){
				return ss[1];
			}
		}
		return null;
	}
	
	//根据演员名查询电影ID
	public int[] getAll (String[] names){//String[] 记不清楚是数组还是字符串了。         return null;               } 
		List<Integer> ids = new ArrayList<Integer>();
		for(String str:s){
			boolean b = true;
			for(String name:names){				
				if(str.indexOf(name)==-1){
					b=false;
				}
			}
			if(b){
				ids.add(Integer.parseInt((str.split(" ")[0])));
			}
		}
		int[] id = new int[ids.size()];
		for(int i = 0;i<id.length;i++){
			id[i] = ids.get(i);
		}
		
		return id;
	}
	
	
	
	
	public static void main(String[] args){
		String[] s = {
				"1 大话西游 周星星 吴孟达 无名",
				"2 人在囧途 王宝强 徐铮 无名"
		};
		Test2 t = new Test2(s);
		//根据ID获取名字
		String name = t.getName(2);
		System.out.println(name);
		String[] names = {"无名1"};
		
		int[] ids = t.getAll(names);
		System.out.println(ids);
	}
}


冰河1258888 2013-05-29
  • 打赏
  • 举报
回复



package test;

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

public class Test2 {
	
	private String[] s;
	
	
	public Test2(String[] s){
		this.s = s;
	}
	
	public String getName(int id){
		for(String str:s){
			String[] ss = str.split(" ");
			if(Integer.parseInt(ss[0])==id){
				return ss[1];
			}
		}
		return null;
	}
	
	//根据演员名查询电影ID
	public int[] getAll (String[] names){//String[] 记不清楚是数组还是字符串了。         return null;               } 
		List<Integer> ids = new ArrayList<Integer>();
		for(String str:s){
			boolean b = true;
			for(String name:names){
				if(!str.contains(name)){
					b=false;
				}
			}
			if(b){
				ids.add(Integer.getInteger(str.split(" ")[0]));
			}
		}
		//Object[] o = ids.toArray();
		int[] id = new int[ids.size()];
		for(int i = 0;i<id.length;i++){
			id[i] = ids.get(i);
		}
		
		return id;
	}
}

冰河1258888 2013-05-29
  • 打赏
  • 举报
回复


public class A {     /**      * 用来初始化电影      * 存储结构:电影ID 电影名字  演员(多个)      * @param s      */    public A(String[] s ){               } //    根据电影id 查找电影名     public String getNane(int id){                   return null;     } //    根据演员名查找电影名。演员要全在电影里面     public int[] getAll (String[] s){//String[] 记不清楚是数组还是字符串了。         return null;               } }  




地下室森林 2013-05-28
  • 打赏
  • 举报
回复
查找ID和电影名应该是静态的,然后里面设有对象数组。。。。个人理解
浅_时光 2013-05-28
  • 打赏
  • 举报
回复
没看懂是什么意思。看来我还是太嫩,去学习了。
dracularking 2013-05-28
  • 打赏
  • 举报
回复
只能用String[]那就自己遍历实现 否则果断List,可以用其containsAll
soton_dolphin 2013-05-28
  • 打赏
  • 举报
回复
 可以试试 Map<String, String[][]> theMap String[][] filmInfo = new String[1][2]; filmInfo[0] = "filemName"; filmInfo[0][0] = "actor1" filmInfo[0][1] = "actor2" theMap.add("filmID', filmInfo)
pxl_1012 2013-05-28
  • 打赏
  • 举报
回复
构造函数就是接受一个数组。你理解的有点浅了。。他是好多电影你。遍历肯定是要有的。。这个我就在想,电影要放在一个什么地方。。
hh_honghui 2013-05-28
  • 打赏
  • 举报
回复
public class A { int id;//电影id String name;//电影名 String[] actors;//演员 public A(int id, String name, String[] actors){ this.id = id; this.name = name; this.actors = actors; } // 根据电影id 查找电影名 public String getName(int id){ if(this.id==id){ return name; } throw new RuntimeException("无此电影"); } // 根据演员名查找电影名。演员要全在电影里面 public String getAll(String[] s){ if(s.equals(actors)){ return name; } throw new RuntimeException("无此电影"); } } 个人理解,仅供参考。
wkcing 2013-05-28
  • 打赏
  • 举报
回复
构造函数的参数这么是字符串数值?

62,616

社区成员

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

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