忍者ブログ

プログラミングの練習

プログラミングの問題やプログラミング関連知識、ソフトウェアのテストについてのブログです

[PR]

×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。


Java List


Java 8 です。
import java.util.ArrayList;
import java.util.List;


public class Main {

 public static void main(String... args) {

     List<String> list = new ArrayList<>();

     list.add("aaa");
     list.add("bbb");
     list.add("ccc");

     for ( String s : list) {
         System.out.println(s);
     }


PR

Java StringBuilder クラス


StringBuilder#setCharAt()で、小文字を大文字に変更してみる例です。

public static void main(String[] args) {
   StringBuilder sb1 = new StringBuilder("abc");

   for(int i =0 ; i < sb1.length() ; i++){
       char c = sb1.charAt(i) ;
       // 大文字への置換
       sb1.setCharAt(i, Character.toUpperCase(c));
   }

   System.out.println(sb1);
 }

実行結果は、
ABC

となります






Mapの利用例


ソースは、こんな感じです。
import java.util.HashMap;
import java.util.Map;

class Person{
Integer id ;
String name ;

 public Person(Integer id, String name) {
   this.id = id;
   this.name = name;
  }
  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 class Main {

public static void main(String[] args) {

  Map<Integer ,Person> map = new HashMap<Integer ,Person>();

  map.put(1, new Person(1, "aaaa")) ;
  map.put(2, new Person(2, "bbbb")) ;
  map.put(3, new Person(3, "cccc")) ;

  //Key を指定して取り出す
  Person pn = map.get(1);
  System.out.println("1件取り出し");
  System.out.println(pn.getId() + " " + pn.getName());

  // 全件取り出す。
  System.out.println("全件取り出し");
  for (Person p : map.values()){
    System.out.println(p.getId() + " " + p.getName());
  }
 }
}

実行結果は、こんな感じです。
1 aaaa
2 bbbb
3 cccc

Java HTTPのヘッダーやコンテンツを読む


こんな感じでしょうか
public static void main(String[] args) throws Exception{

            URL url = new URL("http://localhost:9090/") ;

            URLConnection  connection = url.openConnection() ;

            //httpヘッダーの日付を取得する

            long d = connection.getDate() ;
            if( d != 0) {
                System.out.println(new Date(d));
            }else {
                System.out.println("Date Not Found");
            }

            // Conten type
            System.out.println("Content-Type = " + connection.getContentType());

            //expiration date
            d = connection.getExpiration() ;
            if( d != 0) {
                System.out.println(new Date(d));
            }else {
                System.out.println("Date Not Found");
            }

             // last-modify
            d = connection.getLastModified() ;
            if( d != 0) {
                System.out.println(new Date(d));
            }else {
                System.out.println("Date Not Found");
            }

             // content length
             d = connection.getContentLength() ;
            if( d != 0) {
                System.out.println(d);
            }else {
                System.out.println("Date Not Found");
            }

            // Content
            if( d != 0) {
                InputStream input = connection.getInputStream() ;

                int c ;
                while ((c = input.read()) != -1 ){
                    System.out.println((char)c);
                }
                input.close();
            }

    }