21.08.09

.·2021년 8월 19일
0

playdata

목록 보기
14/35
post-thumbnail

DAY14

charAt(i) : i번째를 찾는 함수 
toCharArray() : String을 char로 바꾸는 함수 
getBytes() : String을 byte[] 로 바꾸는 함수 
Charcter.digit(ch0,10) : ch0의 값이 유니코드로 10진수 

입력받은 문자열을 역순으로 리턴

//1. Char\[]배열로 리턴 받는다. 
char [] res = str.toCharArray();
char [] cv_str = new char[res.length];

//2. 역순으로  for를 이용해서 char[]로 담아 string 생성자로 객체 생성 후 리턴
for (int i = res.length-1; i>=0; i--){
	cv_str[res.length-i-1] = res[i];
    }
    return new String(cv_Str);

소문자의 개수와 대문자의 개수 출력

int upper_cnt = 0; // 대문자의 개수
int lower_cnt = 0; // 소문자의 개수
for (char r : str.toCharArray()){

	if (Character.isUpperCase(r)){
   	upper_cnt++;
       }
   	if (Character.isLowerCase(r)){
       	lower_cnt++;
          }
          
      }
      System.out.println("대문자의 개수 : " + upper_cnt + ", 소문자의 개수 : " + lower_cnt);

공백의 개수

int space_cnt = 0;
if(Character.isSpace(r)) {
space_cnt++;
}

대문자의 개수, 소문자의 개수, 공백의 개수

int upper_cnt = 0;
int lower_cnt = 0;
int space_cnt = 0;
for(char r: str.toCharArray()) {
	int res = Charcter.getType(r);
  switch (res) {
  case Character.SPACE_SEPARATOR:
  	space_cnt ++;
      break;
  case Character.LOWERCASE_LETTER:
  	lower_cnt++;
      break;
  case Character.UPPERCASE_LETTER:
       upper_cnt ++;
       break;
    }
  }

배열을 복사해보자

public static void Prn(){
	String[] copyFrom = { "Affogato" , "Americano" , "Cappuccino", "Correttp" , "Cortado" , "Doppio" , "Espresso" , "Frappucino" , "Freddo" , "Lungo" , "Macchiato" , "Marocchino" , "Ristretto");
    
 //case1   
    String[] copyTO = new String[7];
    System.arraycopy(copyFrom,2,copyTo,0,7);
    
    for(String coffee : copyTo){
    	System.ouy.print(coffee + "");
        }
  
  //case2
    String[] copyTo = java.util.Arrays.copyofRange(copyFrom , 2, 9);
    for (String coffee : copyTo) {
    System.out.print(coffee + " " );
    }
}
 //case3
 public static void Prn03(){
  Address [] ar_address = { new Address ("111","111","111"), 
                            new Address ("222","222","222"),
                            new Address ("333","333","333"),
                            new Address ("444","444","444") }; // 초기크기 4를 변경할 수 없다
  
  Address[] copyTo = java.util.Arrays.copyOfRange(ar_address,1,2);
  for (Address a : copyTo) {
  System.Out.println(a);
  }

Collection interface : util 패키지의 자료 관리형 메소드 CRUD
Collection class : util 패키지의 자료관리형 class들을 관리하는 패키지
대상 컬렉션 클래스들을 명시형으로 또는 묵시형으로 탐색, 정렬 객체가 비어있는지 객체 대상을 원하는 값으로 채우는 등의 기능을 제공한다.
Arrays : 객체 배열 및 기본 자료형들을 배열로 관리하는 클래스

  • list
public static void view_test(){
List <Address> list_all = new ArrayList<>();
Address a1 = new Address("1111","1111","1111");
list_all.add(a1);
list_all.add(new Address("222","222","222"));
list_all.add(new Address("333","333","333"));

Print_List01(list_all);
Print_List02(list_all);
Print_List03(list_all);

}
//출력메소드
//case1
public static void Print_List01(List<Address> ar){
	for(int i = 0; i < ar.size(); i++){
    	Address res = ar.get(i);
        System.out.println(res);
        }
      }
      
//case2
public static void Print_List02(List<Address> ar){
    for (Address res : ar) {
    	System.out.println(res);
        }
      }
      
//case3
public static void Print_List03(List<Address> ar){
	Iterator <Address> res = ar.iterator();
    while (res.hasNext()){
    	System.out.println(res.next());
        }
      }
 public static void main (String[] args){
 	view.test();
  }
  • Set
public static void view_test(){
Set <Address> list_all = new HashSet<>();
Address a1 = new Address("1111","1111","1111");
list_all.add(a1);
list_all.add(new Address("222","222","222"));
list_all.add(new Address("333","333","333"));

Print_List01(list_all);
Print_List02(list_all);
Print_List03(list_all);

}
//출력메소드

//case1
public static void Print_List02(Set <Address> ar){
    for (Address res : ar) {
    	System.out.println(res);
        }
      }
      
//case2
public static void Print_List03(Set<Address> ar){
	Iterator <Address> res = ar.iterator();
    while (res.hasNext()){
    	System.out.println(res.next());
        }
      }
 public static void main (String[] args){
 	view.test();
  }
  • Map
 public class Map_Test{
 public static void View(Map<Integer, Score>res) {
 	System.out.println("1.Map의 키만 리턴받아 출력해보자");
    Set<Integer> key_res = res.KeySet();
    for(Integer r : key_res) {
      System.out.println(r);
     }
     
    System.out.println("2.Value 리턴받아 출력해보자");
    Collection<Score> r = res.values();
    for(Score score_r : r) {
    System.out.println(score_r);
    }
    
    System.out.println("3.Map의 inner class의 타입을 이용해서 key,value 리턴받자");
    Set<Map.Entry<Integer,Score>> n_res = res.entrySet();
    
    for(Map.Entry<Integer,Score> myres : n_res) {
    	System.out.println(myres.getKey() + myres.getValue());
        
   System.out.println("4. 키값이 3인 데이터의 이름을 정길동으로 변경 후 출력"); 
   for(Map.Entry<Integer,Score> myres : n_res) {
   		if(myres.getKey().equals(3)){
           myres.getValue().setName("정길동");
           }
           System.out.println(myres.getKey() + myres.getValue());
           }
           }
 
 public static void main(String[] args) {
 	Map<Integer , Score> map = new HashMap< >();
    map.put(1,new Score("111",90,80,70));
    map.put(1,new Score("222",90,80,70));
    map.put(1,new Score("333",90,80,70));       
    map.put(1,new Score("444",90,80,70)); 
    System.out.prinln(map);
   }
 }
 
  • Util , File
 
 public static void Prn01(){
 	Properties pro = new Properties();
    pro.setProperty("id","bigdata_13");
    pro.setProperty("pw","admin1234);
    System.out.println(pro);
    
    try{
    	pro.store(new FileWriter("my.properties"),"연습용");
        } catch (IOException e) {
        e.printStackTrace();
        }
    
    try{
     pro.storeToXML(new FileOutputStream("my.xml"),"연습용");
     } catch(FileNotFoundException e) {
     e.printStackTrace();
     }catch(IOException e) {
     e.printStackTrace();
     }

class AA {
 @Override
 public String toString(){
 	return "AA [toString()]";
  }
 }
 
   class BB extends AA {
 @Override
 public String toString(){
 	return "BB [toString()]";
  }
 }
 
   class DD extends BB {
 @Override
 public String toString(){
 	return "DD [toString()]";
  }
 }
 
 public class W_Test{
      public static void main (String [] args) {
      	List<? super BB> all = new ArrayList<>();
          //extends 는 선조를제한
          //super는 후손을 제한
          
          //all.add(new AA());
          all.add(new BB());
          all.add(new DD());
          System.out.println(all);
       }
    }
    

0개의 댓글

관련 채용 정보