时间:2021-03-06 10:13:21 | 栏目:Android代码 | 点击:次
概述
找工作挺难,为更好的应对面试,我将一些面试题收集整理起来,好让自己随时复习。
1.请谈一下Android系统的架构。
答:Android系统采用了分层架构,从高层到低层分别是应用程序层、应用程序框架层、系统运行库层和linux核心层。
2.谈谈android大众常用的五种布局。
答:在Android中,共有五种布局方式,分别是:FrameLayout(框架布局),LinearLayout (线性布局),AbsoluteLayout(绝对布局),RelativeLayout(相对布局),TableLayout(表格布局)。
3.谈谈android数据存储方式。
答:Android提供了5种方式存储数据:
4.Android中Activity, Intent, Content Provider, Service各有什么区别。
答:
5.View, surfaceView, GLSurfaceView有什么区别。
答:
6.Adapter有什么作用?常见的Adapter有哪些?
答:
Adapter是连接后端数据和前端显示的适配器接口。常见的Adapter有ArrayAdapter, BaseAdapter, CursorAdapter, HeaderViewListAdapter, ListAdapter, ResourceCursorAdapter, SimpleAdapter, SimpleCursorAdapter, SpinnerAdapter, WrapperListAdapter等
7.Manifest.xml文件中主要包括哪些信息?
答:
8.请写一段代码(SAX, DOM, 或者pull )来解析XML文档。
答:下面是要解析的XML文件:
张三
22 李四
23 定义一个名为Person的javaBean用于存放上面解析出来的xml内容
public class Person { private Integer id; private String name; private Short age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Short getAge() { return age; } public void setAge(Short age) { this.age = age; } } (1)使用SAX读取XML文件;它采用的是事件驱动,并不需要解析完整个文档,速度快并且占用内存少。需要为SAX提供实现ContentHandler接口的类。 PersonDefaultHandler.java import java.util.ArrayList; import java.util.List; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import com.sinber.domain.Person; public class PersonDefaultHandler extends DefaultHandler { private List persons; private Person person ; //记录当前person private String perTag; //记录前一个标签的名称 /** * 重写父类的开始文档方法。用于初始化 */ @Override public void startDocument() throws SAXException { persons = new ArrayList(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if(“person”.equals(localName)){ Integer id = new Integer(attributes.getValue(0)); //取id person = new Person(); person.setId(id); } perTag = localName; } /**参数: * ch 整个XML字符串 * start 节点值在整个XML字符串中的索引位置 * length 节点值的长度 */ @Override public void characters(char[] ch, int start, int length) throws SAXException { if(perTag!=null){ String data = new String(ch,start,length); if(“name”.equals(perTag)){ person.setName(data); }else if(“age”.equals(perTag)){ person.setAge(new Short(data)); } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if(“person”.equals(localName)){ persons.add(person); person = null; } perTag = null; } public List getPersons() { return persons; } } SAXPerson.java import java.io.InputStream; import java.util.List; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import com.sinber.domain.Person; public class SAXPerson{ public static List getPerson() throws Exception{ //通过类装载器获取文件 InputStream inStream = SAXPersonService.class.getClassLoader().getResourceAsStream(“person.xml”); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); PersonDefaultHandler handler = new PersonDefaultHandler(); saxParser.parse(inStream, handler); inStream.close(); return handler.getPersons(); } } (2)DOM解析XML文件时,会将XML文件的所有内容读取到内存中,然后允许您使用DOM API遍历XML树、检索所需的数据。 DOMPerson.java import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import com.sinber.domain.Person; public class DOMPerson { public static List getPerson() throws Exception{ List pers = new ArrayList(); InputStream inStream = SAXPersonService.class.getClassLoader().getResourceAsStream(“person.xml”); DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document dom = builder.parse(inStream); Element root = dom.getDocumentElement(); NodeList persons = root.getElementsByTagName(“person”); for(int i=0;i<persons.getLength();i++){ Element personNode =(Element)persons.item(i); Person person = new Person(); person.setId(new Integer(personNode.getAttribute("id"))); NodeList childNodes = personNode.getChildNodes(); for(int j=0;j<childNodes.getLength();j++){ Node childNode = childNodes.item(j); if(childNode.getNodeType()==Node.ELEMENT_NODE){ Element element = (Element)childNode; if("name".equals(childNode.getNodeName())){ person.setName(new String(element.getFirstChild().getNodeValue())); }else if("age".equals(childNode.getNodeName())){ person.setAge(new Short(element.getFirstChild().getNodeValue())); } } } pers.add(person); } inStream.close(); return pers; } } (3)使用Pull解析器读取XML文件 PullPerson.java import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlSerializer; import android.os.Environment; import android.util.Xml; import com.sinber.domain.Person; public class PullPerson { public static void save(List persons) throws Exception{ XmlSerializer serializer = Xml.newSerializer(); File file = new File(Environment.getExternalStorageDirectory(),”person.xml”); FileOutputStream outStream = new FileOutputStream(file); serializer.setOutput(outStream,”UTF-8″); serializer.startDocument(“UTF-8″, true); serializer.startTag(“”, “persons”); for(Person person:persons){ serializer.startTag(“”, “person”); //person serializer.attribute(“”, “id”, “”+person.getId()); serializer.startTag(“”, “name”); //name serializer.text(person.getName()); serializer.endTag(“”, “name”); //name serializer.startTag(“”, “age”); //age serializer.text(person.getAge().toString()); serializer.endTag(“”, “age”);//age serializer.endTag(“”, “person”); //person } serializer.endTag(“”, “persons”); serializer.endDocument(); outStream.close(); } public static List getPersons() throws Exception{ List persons = null; Person person = null; XmlPullParser parser= Xml.newPullParser(); InputStream inStream = PullPersonService.class.getClassLoader().getResourceAsStream(“person.xml”); parser.setInput(inStream, “UTF-8″); int eventType = parser.getEventType(); //触发第一个事件 while(eventType!=XmlPullParser.END_DOCUMENT){ switch(eventType){ case XmlPullParser.START_DOCUMENT: persons = new ArrayList(); break; case XmlPullParser.START_TAG: //开始元素事件 if(“person”.equals(parser.getName())){ person = new Person(); person.setId(new Integer(parser.getAttributeValue(0))); }else if(person!=null){ if(“name”.equals(parser.getName())){ person.setName(parser.nextText()); }else if(“age”.equals(parser.getName())){ person.setAge(new Short(parser.nextText())); } } break; case XmlPullParser.END_TAG: //结束元素事件 if(“person”.equals(parser.getName())){ persons.add(person); person = null; } break; default: break; } eventType = parser.next(); } return persons; } } 以上三种方式任选其一即可。
9.根据自己的理解描述下Android数字签名。
答:
10.已知单链表的头结构head,写一个函数把这个链表逆序。
答: 如下所示
Node.java public class Node { private Integer count; private Node nextNode; public Node(){ } public Node(int count){ this.count = new Integer(count); } public Integer getCount() { return count; } public void setCount(Integer count) { this.count = count; } public Node getNextNode() { return nextNode; } public void setNextNode(Node nextNode) { this.nextNode = nextNode; } } ReverseSingleLink.java public class ReverseSingleLink { public static Node revSingleLink(Node head){ if(head == null){ //链表为空不能逆序 return head; } if(head.getNextNode()==null){ //如果只有一个结点,当然逆过来也是同一个 return head; } Node rhead = revSingleLink(head.getNextNode()); head.getNextNode().setNextNode(head); head.setNextNode(null); return rhead; } public static void main(String[] args){ Node head = new Node(0); Node temp1 = null,temp2 = null; for(int i=1;i<100;i++){ temp1 = new Node(i); if(i==1){ head.setNextNode(temp1); }else{ temp2.setNextNode(temp1); } temp2 = temp1; } head = revSingleLink(head); while(head!=null){ head = head.getNextNode(); } } }
总结