(1) write NameDao interface contains the following methods :
/ / the array of strings into a list , this method requires the use of Iterator interface , see textbooks P558
public ListgetNameFromArray (String [] names);
/ / search in the list of items with the same name , it returns the name where the serial number, find returns -1.
public int searchName (ListnameList, String name);
/ / the specified id, removed from the list specified in the project if id specified error ( negative or beyond the list of maximum ) throws IllegaArgumentException exception , removing Returns true if successful
public boolean removeFromList (ListnameList, int id);
(2) write NameDaoImpl achieve NameDao interface.
(3) write a test class to test the functionality of the code .
Neighborhoods code ? ?
ps: those who want to write my own good , do not send , and I can not write it because to ask, I also continued to write code, I hope to be able to reply to solve the problem .
------ Solution ---------------------------------------- ----
import java.util.List;
public interface NameDao {
public List<String> getNameFromArray(String[] names);
public boolean removeFromList(List<String> ListnameList,int id);
}
import java.util.ArrayList;
import java.util.List;
public class NameDaoImpl implements NameDao {
//实现getNameFromArray方法
@Override
public List<String> getNameFromArray(String[] names){
List<String> list=new ArrayList<String>();
//遍历names数组
for (String name:names){
//添加元素
list.add(name);
}
return list;
}
@Override
public boolean removeFromList(List<String> ListnameList,int id)throws IllegalArgumentException{
boolean flag=false;
if(id>=ListnameList.size()||id<0){
throw new IllegalArgumentException();
}
if(ListnameList.remove(id)!=null){
flag=true;
}
return flag;
}
}
import java.util.List;
public class Test {
public static void main(String[] args) throws IllegalArgumentException{
//创建names数组
String[] names={"张三","李四","王五"};
NameDao nameDao=new NameDaoImpl();
//创建List并赋值
List<String> list=nameDao.getNameFromArray(names);
int id=2;
if(nameDao.removeFromList(list,id)){
System.out.println("删除成功");
}
}
}
------ Solution ------------------------------------- -------
hope is that you want.
package com.test1;
import java.util.List;
public interface NameDao {
// 将数组中的字符串放入列表,该方法需要使用Iterator接口实现,参见课本P558
public List ListgetNameFromArray(String[] names);
// 在列表中搜索到与name相同的项目,就返回该name所在的序号,找不到返回-1。
public int searchName(List nameList, String name);
/*根据指定id,从列表中移除指定的项目,如果id指定错误
(负数或者超出列表最大值)抛出IllegaArgumentException异常,移除成功返回true*/
public boolean removeFromList(List nameList, int id) throws Exception;
}
package com.test1;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class NameDaoImpl implements NameDao {
public List ListgetNameFromArray(String[] names) {
List<String> list = new ArrayList<String>();
for (String name : names) {
list.add(name);
}
// 使用迭代器输出
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
return list;
}
public int searchName(List nameList, String name) {
for (int i = 0; i < nameList.size(); i++) {
if (name.equals(nameList.get(i))) {
return i;
}
}
return -1;
}
public boolean removeFromList(List nameList, int id) throws Exception{
try {
nameList.remove(id);
return true;
} catch (RuntimeException e) {
// TODO Auto-generated catch block
throw new Exception("IllegaArgumentException");
}
}
}
package com.test1;
import java.util.List;
public class Test {
/**
* @author gaopeng
* 2013-11-19
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
NameDao nd = new NameDaoImpl();
System.out.println("****将数组中的字符串放入列表,该方法需要使用Iterator接口实现**");
String[] strs = new String[] { "鸣人", "佐助", "卡卡西" };
List list = nd.ListgetNameFromArray(strs);
System.out.println("\n****在列表中搜索到与name相同的项目,就返回该name所在的序号,找不到返回-1**");
System.out.println(nd.searchName(list, "卡卡西"));
try {
System.out.println("\n****根据指定id,从列表中移除指定的项目 ..... **");
System.out.println(nd.removeFromList(list, 0));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
------ Solution ------------------------------------- -------
Interface:
package com.meritit.dm.csdn.ext;
import java.util.List;
/**
* NameDao
*
* @author 袁慎建
* @version 1.0
* @email ysjian_pingcx@126.com
* @QQ 646633781
* @telephone 18192235667
* @csdnBlog http://blog.csdn.net/ysjian_pingcx
* @createTime 2013-11-19 下午5:37:55
* @copyRight 西安美林电子有限责任公司
*/
public interface INameDao {
/**
* 从数组中获取名字列表
*
* @param names
* 名字数组
* @return 返回一个集合列表
*/
List<String> getNameFromArray(String[] names);
/**
* 从列表中移除指定id的元素
*
* @param nameList
* 指定列表列表
* @param id
* 指定的id
* @return 如果移除成功返回<code>true</code>,否则返回<code>false</code>
*/
boolean removeFromList(List<String> nameList, int id);
/**
* 在列表中搜索到与name相同的项目,就返回该name所在的序号,找不到返回-1。
*
* @param nameList
* 指定列表
* @param name
* 匹配值
* @return 返回搜索到的序号
*/
int searchName(List<String> nameList, String name);
}
implementation class and Test:
public class NameDaoImpl implements INameDao {
@Override
public List<String> getNameFromArray(String[] names) {
if (names == null) {
throw new NullPointerException("null value input");
}
List<String> returnList = new ArrayList<String>();
for (String name : names) {
returnList.add(name);
}
return returnList;
}
@Override
public boolean removeFromList(List<String> nameList, int id) {
if (nameList == null) {
throw new NullPointerException("null value input");
}
if (id > nameList.size()) {
throw new IllegalArgumentException(
"id is higher than the size of list-->[id=" + id + "]");
}
if (id < 0) {
throw new IllegalArgumentException("id is lower than zero-->[id="
+ id + "]");
}
return nameList.remove(nameList.get(id));
}
@Override
public int searchName(List<String> nameList, String name) {
if (nameList == null) {
throw new NullPointerException("null value input");
}
if (name == null) {
throw new NullPointerException("null value input");
}
return nameList.indexOf(name) + 1;
}
public static void main(String[] args) throws IllegalArgumentException {
// 创建names数组
String[] names = { "ysjian001", "ysjian002", "ysjian003", "ysjian004",
"ysjian005" };
INameDao nameDao = new NameDaoImpl();
// 创建List并赋值
List<String> list = nameDao.getNameFromArray(names);
int id = 3;
if (nameDao.removeFromList(list, id)) {
System.out.println("删除成功");
}
System.out.println(list);
}
}
没有评论:
发表评论