java程序员面试题

1.把字符数组String str[] = {"6","5","3","1","2","4"}中的字符数字按照从小到大调整位置。
package cn.test;

public class SortTest {

	public static void main(String[] args) {
		String str[] = {"6","5","3","1","2","4"};
		for(int i=0;i<str.length-1;i++){
			for(int j=i+1;j<str.length;j++){
				if(Integer.parseInt(str[i])>Integer.parseInt(str[j])){
					String st = str[i];
					str[i] = str[j];
					str[j] = st;
				}				
			}
		}
		for(String st : str){
			System.out.println(st);
		}
	}
}

2、完成下面方格,使每一个方格相加相等。

3、编程用SAX方式解析XML,XML文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<person>
<name>王小明</name>
<college>信息学院</college>
<telephone>6258113</telephone>
<notes>男,1955年生,博士,95年调入海南大学</notes>
</person>

读取XML文件:
package cn.test;

public class Person {
	private String name;
	private String college;
	private String telephone;
	private short age;
	private int id;
	
	public Person() {
	
	}

	public String toString() {
		return "Person [name=" + name + ", college=" + college + ", telephone="
				+ telephone + ", notes=" + notes + "]";
	}

	private String notes;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCollege() {
		return college;
	}

	public void setCollege(String college) {
		this.college = college;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getNotes() {
		return notes;
	}

	public void setNotes(String notes) {
		this.notes = notes;
	}

	public short getAge() {
		return age;
	}

	public void setAge(short age) {
		this.age = age;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

}

package cn.test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class TestReadXml extends DefaultHandler {
	private List<Person> persons;
	private Person person;
	private String preTag;

	public void startDocument() throws SAXException {
		persons = new ArrayList<Person>();
	}

	List<Person> getPersons() {
		return persons;
	}

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (person != null) {
			String data = new String(ch, start, length);
			if ("name".equals(preTag)) {
				person.setName(data);
			} else if ("college".equals(preTag)) {
				person.setCollege(data);
			} else if ("telephone".equals(preTag)) {
				person.setTelephone(data);
			} else if ("notes".equals(preTag)) {
				person.setNotes(data);
			}
		}
	}

	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (person != null && "person".equals(qName)) {
			persons.add(person);
			person = null;
		}
		preTag = null;
	}

	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if ("person".equals(qName)) {
			person = new Person();
		}
		preTag = qName;
	}

	public static List<Person> readXml(InputStream inputStream)
			throws Exception {
		SAXParserFactory spf = SAXParserFactory.newInstance();
		SAXParser saxParser = spf.newSAXParser();

		TestReadXml handler = new TestReadXml();
		saxParser.parse(inputStream, handler);
		inputStream.close();
		return handler.getPersons();
	}

	public static void main(String[] args) throws Exception {
		InputStream inputStream = TestReadXml.class.getClassLoader()
				.getResourceAsStream("person.xml");
		List<Person> persons = readXml(inputStream);
		for (Person person : persons) {
			System.out.println(person.toString());
		}

	}
}

4、完成foo()函数的内容,要求能够弹出对话框中的选中的是第几个单选框。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function foo(){
	//在此处添加代码
	var radioGroup = document.getElementsByName("radioGroup");
	for(var i=0;i<radioGroup.length;i++){
		if(radioGroup[i].checked){
			alert(i+1);
		}
	}	
	return false;
}
</script>
</head>
<body>
<form action="form1" onsubmit="return foo();">
<input type="radio" name="radioGroup" />
<input type="radio" name="radioGroup" />
<input type="radio" name="radioGroup" />
<input type="radio" name="radioGroup" />
<input type="radio" name="radioGroup" />
<input type="radio" name="radioGroup" />
<input type="submit">
</form>
</body>
</html>

猜你喜欢

转载自fly-sky.iteye.com/blog/1044042