Coder Social home page Coder Social logo

string's Introduction

Java String api

나는 스트링이라네~~~

String Api

  1. length - 문자열의 길이를 구한다.
String a="string/Test";
System.out.println(a.length());
  1. indexof - 문자열의 위치를 리턴한다.
System.out.println(a.indexOf("T"));

Math Api

수학 함수

  1. round -값을 반올림
Math.round(123.5);
  1. 반올림
long b=Math.round(123.5);
System.out.println(b);
  1. 올림
System.out.println((double)Math.ceil(343.1543));
  1. 내림
System.out.println((double)Math.floor((double)563.8));
  1. 랜덤
Math.random();
Random random=new Random();
random.nextInt(100);

Algorithm

  1. 짝수 더하기
public long sumEvenNumber2(long max){
  long result=0;
  if(max%2==0){
    result=max/2*max/2+max/2;
  }else{
    result=((max+1)/2)*((max+1)/2)+((max+1)/2);
  }
  return result;
}
  1. 홀수 더하기
public long sumOddNumber(long max){
  long sum=0;
  
  //짝수일때 
  if(max%2==0){
    sum=max*max/2/2;
  }
  else{
    sum=(max+1)*(max/2+1)/2;
  }
  return sum;
}
  1. 로또번호 구하기
public static void getLottoNumbers(){
  int[] result=new int[6];
  boolean flag=false;
  Random random=new Random();
  
  // 동적배열 - 크기를 제한하지 않는 객체 배열
  // 종류:List, Set, Map
  // List = 인덱스와 값을 가지고 있는 배열
  // Set = 키와값을 가지고 있는 동적 배열, 값의 중복이허용됨 
  // Map map=new HashMap();
  // map.put("키", 33);
  for(int i=0; i<6; i++){
    
    //random 함수에서 발생되는 난수는 중복 될 수 있다.
    int temp=random.nextInt(45)+1;
    
    // 중복값에 대한 예외처리
    for(int j=0; j<result.length; j++){
      if(temp!=result[i]){
        flag=true;
        break;
      }
    }
    if(flag){
      result[i]=temp;
    }
  }
  for(int i=0; i<6; i++){
    System.out.print(result[i] +" ");
  }
}
  1. 아나그램 알고리즘
public static boolean checkAnagram(String a, String b){
  a.toLowerCase();
  b.toLowerCase();
  a=a.replace(" ", "");
  b=b.replace(" ", "");
  
  String temp="";
  char[] array;
  array=b.toCharArray();
  if(a.length()==b.length()){
    for(int i = 0; i<a.length(); i++){
      for(int j=0; j<b.length(); j++){
        if(a.charAt(i)==array[j]){						
          temp=temp+array[j];
          array[j]=' ';
          break;
        }
      }
    }

    for(int i=0; i<array.length; i++){
      System.out.print(array[i]);
    }
    return (temp.equals(a));
  }
  else{
    return false;
  }
}

string's People

Contributors

nyagum avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.