//将字符串转换成字节数组
public static void day4() {
byte[] byBuffer = new byte[200];
String str = "abcdefg";
byBuffer =str.getBytes();
System.out.println(byBuffer);
}
//字节数组转换为字符串
public static void day5() {
byte[] by = new byte[200];
String strR = new String(by);
strR = String.copyValueOf(strR.toCharArray(), 0, by.length);
System.out.println(strR);
}
java 字符串转换成document 取元素:
byte[] bytes=new String("123456").getBytes(); //String.getBytes()返回一 个字节数组。
ByteArrayInputStream bin=new ByteArrayInputStream(bytes);
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document=duilder.parse(bin); NodeList nodeList=document.getElementByTagName("abc");
String value=nodeList.item(0).getTextContent(); //这样就可以取得原本字符串里的xml元素了
ArrayList<News> list = new ArrayList<News>();
list.add(new News(1,"list1", "a"));
list.add(new News(2,"list2", "b"));
list.add(new News(3,"list3", "c"));
list.add(new News(4,"list4", "d"));
//第一种、最基础的遍历方式:for循环,指定下标长度,使用List集合的size()方法,进行for循环遍历
// for(int i=0;i<list.size();i++) {
// News s = list.get(i);
// System.out.println(s.getId()+" "+s.getTitle()+" "+s.getAuthor());
// }
//第二种、较为简洁的遍历方式:使用foreach遍历List,但不能对某一个元素进行操作(这种方法在遍历数组和Map集合的时候同样适用)
// for(News s:list) {
// System.out.println(s.getId()+" "+s.getTitle()+" "+s.getAuthor());
//
// }
//适用迭代器Iterator遍历:直接根据List集合的自动遍历
Iterator<News> iter = list.iterator();
while (iter.hasNext()) {
News s = (News) iter.next();
System.out.println(s.getId()+" "+s.getTitle()+" "+s.getAuthor());
}
}