나는 스트링이라네~~~
- length - 문자열의 길이를 구한다.
String a="string/Test";
System.out.println(a.length());
- indexof - 문자열의 위치를 리턴한다.
System.out.println(a.indexOf("T"));
수학 함수
- round -값을 반올림
Math.round(123.5);
- 반올림
long b=Math.round(123.5);
System.out.println(b);
- 올림
System.out.println((double)Math.ceil(343.1543));
- 내림
System.out.println((double)Math.floor((double)563.8));
- 랜덤
Math.random();
Random random=new Random();
random.nextInt(100);
- 짝수 더하기
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;
}
- 홀수 더하기
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;
}
- 로또번호 구하기
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] +" ");
}
}
- 아나그램 알고리즘
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;
}
}