1364 lines
74 KiB
HTML
1364 lines
74 KiB
HTML
<!DOCTYPE HTML>
|
||
<html lang="en" class="coal" dir="ltr">
|
||
<head>
|
||
<!-- Book generated using mdBook -->
|
||
<meta charset="UTF-8">
|
||
<title>java grammar and codewar - Andrew's Blog</title>
|
||
|
||
|
||
<!-- Custom HTML head -->
|
||
|
||
<meta name="description" content="Andrew Ryan's Blog">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<meta name="theme-color" content="#ffffff">
|
||
|
||
<link rel="icon" href="../../favicon.svg">
|
||
<link rel="shortcut icon" href="../../favicon.png">
|
||
<link rel="stylesheet" href="../../css/variables.css">
|
||
<link rel="stylesheet" href="../../css/general.css">
|
||
<link rel="stylesheet" href="../../css/chrome.css">
|
||
|
||
<!-- Fonts -->
|
||
<link rel="stylesheet" href="../../FontAwesome/css/font-awesome.css">
|
||
<link rel="stylesheet" href="../../fonts/fonts.css">
|
||
|
||
<!-- Highlight.js Stylesheets -->
|
||
<link rel="stylesheet" href="../../highlight.css">
|
||
<link rel="stylesheet" href="../../tomorrow-night.css">
|
||
<link rel="stylesheet" href="../../ayu-highlight.css">
|
||
|
||
<!-- Custom theme stylesheets -->
|
||
<link rel="stylesheet" href="../../src/style/custom.css">
|
||
|
||
<!-- MathJax -->
|
||
<script async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
|
||
</head>
|
||
<body class="sidebar-visible no-js">
|
||
<div id="body-container">
|
||
<!-- Provide site root to javascript -->
|
||
<script>
|
||
var path_to_root = "../../";
|
||
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "coal" : "coal";
|
||
</script>
|
||
|
||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
||
<script>
|
||
try {
|
||
var theme = localStorage.getItem('mdbook-theme');
|
||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
||
|
||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
||
}
|
||
|
||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
||
}
|
||
} catch (e) { }
|
||
</script>
|
||
|
||
<!-- Set the theme before any content is loaded, prevents flash -->
|
||
<script>
|
||
var theme;
|
||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
||
if (theme === null || theme === undefined) { theme = default_theme; }
|
||
var html = document.querySelector('html');
|
||
html.classList.remove('coal')
|
||
html.classList.add(theme);
|
||
var body = document.querySelector('body');
|
||
body.classList.remove('no-js')
|
||
body.classList.add('js');
|
||
</script>
|
||
|
||
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
|
||
|
||
<!-- Hide / unhide sidebar before it is displayed -->
|
||
<script>
|
||
var body = document.querySelector('body');
|
||
var sidebar = null;
|
||
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
|
||
if (document.body.clientWidth >= 1080) {
|
||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
||
sidebar = sidebar || 'visible';
|
||
} else {
|
||
sidebar = 'hidden';
|
||
}
|
||
sidebar_toggle.checked = sidebar === 'visible';
|
||
body.classList.remove('sidebar-visible');
|
||
body.classList.add("sidebar-" + sidebar);
|
||
</script>
|
||
|
||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
||
<div class="sidebar-scrollbox">
|
||
<ol class="chapter"><li class="chapter-item affix "><a href="../../index.html">Andrew's Blog</a></li><li class="chapter-item "><a href="../../posts/linux/linux.html"><strong aria-hidden="true">1.</strong> linux</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/linux/install_linux.html"><strong aria-hidden="true">1.1.</strong> install linux</a></li><li class="chapter-item "><a href="../../posts/linux/bash_profile.html"><strong aria-hidden="true">1.2.</strong> bash profile</a></li><li class="chapter-item "><a href="../../posts/linux/command_list.html"><strong aria-hidden="true">1.3.</strong> command list</a></li><li class="chapter-item "><a href="../../posts/linux/git_guide.html"><strong aria-hidden="true">1.4.</strong> git guide</a></li><li class="chapter-item "><a href="../../posts/linux/tar.html"><strong aria-hidden="true">1.5.</strong> tar</a></li><li class="chapter-item "><a href="../../posts/linux/run_x86_elf_in_x64_setup.html"><strong aria-hidden="true">1.6.</strong> run x86 elf in x64 setup</a></li></ol></li><li class="chapter-item "><a href="../../posts/mac/mac.html"><strong aria-hidden="true">2.</strong> mac</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/mac/macos_profiles.html"><strong aria-hidden="true">2.1.</strong> macos profiles</a></li></ol></li><li class="chapter-item "><a href="../../posts/swift/swift.html"><strong aria-hidden="true">3.</strong> swift</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/swift/learn_swift.html"><strong aria-hidden="true">3.1.</strong> learn swift basics</a></li><li class="chapter-item "><a href="../../posts/swift/swift_extensions.html"><strong aria-hidden="true">3.2.</strong> Swift extensions</a></li><li class="chapter-item "><a href="../../posts/swift/swiftui_extension.html"><strong aria-hidden="true">3.3.</strong> SwiftUI extensions</a></li><li class="chapter-item "><a href="../../posts/swift/install_swift.html"><strong aria-hidden="true">3.4.</strong> install swift</a></li><li class="chapter-item "><a href="../../posts/swift/task_planner.html"><strong aria-hidden="true">3.5.</strong> implment task panner app with SwiftUI</a></li><li class="chapter-item "><a href="../../posts/swift/swift_cheat_sheet.html"><strong aria-hidden="true">3.6.</strong> Swift Cheat Sheet</a></li><li class="chapter-item "><a href="../../posts/swift/yinci_url.html"><strong aria-hidden="true">3.7.</strong> Personal privacy protocol</a></li><li class="chapter-item "><a href="../../posts/swift/swift_regular_exressions.html"><strong aria-hidden="true">3.8.</strong> Swift regular exressions</a></li><li class="chapter-item "><a href="../../posts/ios/how_to_create_beautiful_ios_charts_in_swift.html"><strong aria-hidden="true">3.9.</strong> How to Create Beautiful iOS Charts in鑱絊wift</a></li><li class="chapter-item "><a href="../../posts/swift/swiftui_source_code.html"><strong aria-hidden="true">3.10.</strong> SwiftUI source code</a></li><li class="chapter-item "><a href="../../posts/swift/use_swift_fetch_iciba_api.html"><strong aria-hidden="true">3.11.</strong> use swift fetch iciba API</a></li></ol></li><li class="chapter-item "><a href="../../posts/ios/ios.html"><strong aria-hidden="true">4.</strong> ios</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/ios/cocaposd_setup_and_install_for_ios_project.html"><strong aria-hidden="true">4.1.</strong> cocaposd setup and install for ios project</a></li><li class="chapter-item "><a href="../../posts/ios/swiftui_show_gif_image.html"><strong aria-hidden="true">4.2.</strong> SwiftUI show gif image</a></li><li class="chapter-item "><a href="../../posts/ios/implement_task_planner_app.html"><strong aria-hidden="true">4.3.</strong> implement Task planner App</a></li></ol></li><li class="chapter-item "><a href="../../posts/objective_c/objective_c.html"><strong aria-hidden="true">5.</strong> objective_c</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/objective_c/objective_c_cheat_sheet.html"><strong aria-hidden="true">5.1.</strong> Objective-C Cheat Sheet</a></li><li class="chapter-item "><a href="../../posts/objective_c/objective_c_for_absolute_beginners_read_note.html"><strong aria-hidden="true">5.2.</strong> Objective-C Note</a></li></ol></li><li class="chapter-item "><a href="../../posts/dart/dart.html"><strong aria-hidden="true">6.</strong> dart</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/dart/flutter.html"><strong aria-hidden="true">6.1.</strong> Flutter Cheat Sheet</a></li><li class="chapter-item "><a href="../../posts/dart/dart_cheat_sheet.html"><strong aria-hidden="true">6.2.</strong> Dart Cheat Sheet</a></li><li class="chapter-item "><a href="../../posts/flutter/flutter_dev_test.html"><strong aria-hidden="true">6.3.</strong> Flutter dev test</a></li></ol></li><li class="chapter-item "><a href="../../posts/rust/rust.html"><strong aria-hidden="true">7.</strong> rust</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/rust/offline_use_rust.html"><strong aria-hidden="true">7.1.</strong> Offline use rust</a></li><li class="chapter-item "><a href="../../posts/rust/rust_grammer.html"><strong aria-hidden="true">7.2.</strong> rust grammar</a></li><li class="chapter-item "><a href="../../posts/rust/pase_string_and_decimal_conversion.html"><strong aria-hidden="true">7.3.</strong> pase string and decimal conversion</a></li><li class="chapter-item "><a href="../../posts/rust/parse_types.html"><strong aria-hidden="true">7.4.</strong> rust types</a></li><li class="chapter-item "><a href="../../posts/rust/rust_life_cycle.html"><strong aria-hidden="true">7.5.</strong> Rust life cycle</a></li><li class="chapter-item "><a href="../../posts/rust/rust_generic.html"><strong aria-hidden="true">7.6.</strong> rust generics</a></li><li class="chapter-item "><a href="../../posts/rust/rust_implment_matrix.html"><strong aria-hidden="true">7.7.</strong> Rust implement matrix</a></li><li class="chapter-item "><a href="../../posts/rust/rust_sort.html"><strong aria-hidden="true">7.8.</strong> Rust implement sort algorithms</a></li><li class="chapter-item "><a href="../../posts/rust/implement_aes_encryption.html"><strong aria-hidden="true">7.9.</strong> Rust implement AEC encryption and decryption</a></li><li class="chapter-item "><a href="../../posts/rust/implement_trie_data_structure.html"><strong aria-hidden="true">7.10.</strong> implement trie data structure</a></li><li class="chapter-item "><a href="../../posts/rust/rust_implement_tree.html"><strong aria-hidden="true">7.11.</strong> implement tree data_structure</a></li><li class="chapter-item "><a href="../../posts/rust/list_dir.html"><strong aria-hidden="true">7.12.</strong> list dir</a></li><li class="chapter-item "><a href="../../posts/rust/fast_way_to_implment_object_trait.html"><strong aria-hidden="true">7.13.</strong> fast way to implment object trait</a></li><li class="chapter-item "><a href="../../posts/rust/compress_rust_binary_size.html"><strong aria-hidden="true">7.14.</strong> compress rust binary size</a></li><li class="chapter-item "><a href="../../posts/rust/implment_file_upload_backend.html"><strong aria-hidden="true">7.15.</strong> impliment file upload</a></li><li class="chapter-item "><a href="../../posts/rust/this_is_add_post_cli_implementation_in_rust.html"><strong aria-hidden="true">7.16.</strong> this is add_post cli implementation in rust</a></li><li class="chapter-item "><a href="../../posts/rust/use_rust_implment_a_copyclipbord_cli.html"><strong aria-hidden="true">7.17.</strong> Use rust implment a copyclipbord CLI</a></li><li class="chapter-item "><a href="../../posts/rust/sqlite_database_add_delete_update_show_in_rust.html"><strong aria-hidden="true">7.18.</strong> sqlite database add delete update show in rust</a></li><li class="chapter-item "><a href="../../posts/rust/implementing_tokio_joinhandle_for_wasm.html"><strong aria-hidden="true">7.19.</strong> Implementing tokio JoinHandle for wasm</a></li><li class="chapter-item "><a href="../../posts/rust/rust_implement_a_crate_for_encode_and_decode_brainfuck_and_ook.html"><strong aria-hidden="true">7.20.</strong> rust implement a crate for encode and decode brainfuck and ook</a></li><li class="chapter-item "><a href="../../posts/rust/slint_builtin_elements.html"><strong aria-hidden="true">7.21.</strong> Slint Builtin Elements</a></li><li class="chapter-item "><a href="../../posts/rust/corporate_network_install_rust_on_windows.html"><strong aria-hidden="true">7.22.</strong> Corporate network install Rust on windows</a></li><li class="chapter-item "><a href="../../posts/rust/rust_binary_file_how_to_judge_static_link_or_dynamic_link_in_macos.html"><strong aria-hidden="true">7.23.</strong> rust binary file how to judge static link or dynamic link in Macos</a></li><li class="chapter-item "><a href="../../posts/rust/rust_binary_include_dir_and_get_contents.html"><strong aria-hidden="true">7.24.</strong> rust binary include dir and get contents</a></li><li class="chapter-item "><a href="../../posts/rust/rust_logger_non-block.html"><strong aria-hidden="true">7.25.</strong> rust logger non-block</a></li><li class="chapter-item "><a href="../../posts/rust/rust_connect_sql_server_database.html"><strong aria-hidden="true">7.26.</strong> rust connect sql server database</a></li><li class="chapter-item "><a href="../../posts/rust/rust_websocket_implment.html"><strong aria-hidden="true">7.27.</strong> rust websocket implment</a></li></ol></li><li class="chapter-item expanded "><a href="../../posts/java/java.html"><strong aria-hidden="true">8.</strong> java</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../posts/java/java_grammar.html" class="active"><strong aria-hidden="true">8.1.</strong> java grammar and codewar</a></li><li class="chapter-item "><a href="../../posts/java/run_jar.html"><strong aria-hidden="true">8.2.</strong> java run .jar</a></li><li class="chapter-item "><a href="../../posts/java/java_pomxml_add_defaultgoal_to_build.html"><strong aria-hidden="true">8.3.</strong> Java pomxml add defaultGoal to build</a></li><li class="chapter-item "><a href="../../posts/java/java_set_mvn_mirror.html"><strong aria-hidden="true">8.4.</strong> Java set mvn mirror</a></li></ol></li><li class="chapter-item "><a href="../../posts/python/python.html"><strong aria-hidden="true">9.</strong> python</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/python/convert_pesn.html"><strong aria-hidden="true">9.1.</strong> convert pesn</a></li><li class="chapter-item "><a href="../../posts/python/find_remove_dir.html"><strong aria-hidden="true">9.2.</strong> find and remove dir</a></li><li class="chapter-item "><a href="../../posts/python/timing_message.html"><strong aria-hidden="true">9.3.</strong> wechat send message</a></li><li class="chapter-item "><a href="../../posts/python/use_python_openpyxl_package_read_and_edit_excel_files.html"><strong aria-hidden="true">9.4.</strong> Use python openpyxl package read and edit excel files</a></li></ol></li><li class="chapter-item "><a href="../../posts/go/go.html"><strong aria-hidden="true">10.</strong> go</a></li><li class="chapter-item "><a href="../../posts/js/js.html"><strong aria-hidden="true">11.</strong> js</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/js/js_tutorial.html"><strong aria-hidden="true">11.1.</strong> js tutorial</a></li><li class="chapter-item "><a href="../../posts/js/js_tutorial_map.html"><strong aria-hidden="true">11.2.</strong> ja map</a></li><li class="chapter-item "><a href="../../posts/js/js_tutorial_math.html"><strong aria-hidden="true">11.3.</strong> js math</a></li><li class="chapter-item "><a href="../../posts/js/js_tutorial_object.html"><strong aria-hidden="true">11.4.</strong> js object</a></li><li class="chapter-item "><a href="../../posts/js/js_tutorial_set.html"><strong aria-hidden="true">11.5.</strong> js set</a></li><li class="chapter-item "><a href="../../posts/js/single_thread_and_asynchronous.html"><strong aria-hidden="true">11.6.</strong> single thread and asynchronous</a></li><li class="chapter-item "><a href="../../posts/js/this.html"><strong aria-hidden="true">11.7.</strong> js this</a></li><li class="chapter-item "><a href="../../posts/js/js_implment_aes.html"><strong aria-hidden="true">11.8.</strong> js implment aes</a></li><li class="chapter-item "><a href="../../posts/js/getting_started_with_ajax.html"><strong aria-hidden="true">11.9.</strong> getting started with ajax</a></li><li class="chapter-item "><a href="../../posts/js/BinarySearchTree.html"><strong aria-hidden="true">11.10.</strong> binary search tree</a></li><li class="chapter-item "><a href="../../posts/js/goole_zx.html"><strong aria-hidden="true">11.11.</strong> goole zx</a></li><li class="chapter-item "><a href="../../posts/js/es6.html"><strong aria-hidden="true">11.12.</strong> es6</a></li></ol></li><li class="chapter-item "><a href="../../posts/ruby/ruby.html"><strong aria-hidden="true">12.</strong> ruby</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/ruby/rails_setup_env.html"><strong aria-hidden="true">12.1.</strong> ruby on rails setup environment</a></li><li class="chapter-item "><a href="../../posts/ruby/learn_ruby.html"><strong aria-hidden="true">12.2.</strong> learn ruby</a></li><li class="chapter-item "><a href="../../posts/ruby/ruby_note.html"><strong aria-hidden="true">12.3.</strong> Ruby Note</a></li><li class="chapter-item "><a href="../../posts/ruby/setup_ruby_for_ctf.html"><strong aria-hidden="true">12.4.</strong> Setup ruby for CTF</a></li></ol></li><li class="chapter-item "><a href="../../posts/react/react.html"><strong aria-hidden="true">13.</strong> react</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/react/react_life_cycle.html"><strong aria-hidden="true">13.1.</strong> react life cycle</a></li><li class="chapter-item "><a href="../../posts/react/react_router.html"><strong aria-hidden="true">13.2.</strong> react router</a></li><li class="chapter-item "><a href="../../posts/react/react_this.html"><strong aria-hidden="true">13.3.</strong> react this</a></li><li class="chapter-item "><a href="../../posts/react/react_interviw.html"><strong aria-hidden="true">13.4.</strong> react interview</a></li><li class="chapter-item "><a href="../../posts/react/important_react_interview.html"><strong aria-hidden="true">13.5.</strong> important react interview</a></li><li class="chapter-item "><a href="../../posts/react/react_quick_reference.html"><strong aria-hidden="true">13.6.</strong> react quick reference</a></li><li class="chapter-item "><a href="../../posts/react/redux_quick_reference.html"><strong aria-hidden="true">13.7.</strong> redux quick reference</a></li></ol></li><li class="chapter-item "><a href="../../posts/vue/vue.html"><strong aria-hidden="true">14.</strong> vue</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/vue/vue_ajax.html"><strong aria-hidden="true">14.1.</strong> vue ajax</a></li></ol></li><li class="chapter-item "><a href="../../posts/angular/angular.html"><strong aria-hidden="true">15.</strong> angular</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/angular/controller_communication.html"><strong aria-hidden="true">15.1.</strong> controller communication</a></li><li class="chapter-item "><a href="../../posts/angular/creating_custom_directives.html"><strong aria-hidden="true">15.2.</strong> creating custom directives</a></li><li class="chapter-item "><a href="../../posts/angular/directive_notes.html"><strong aria-hidden="true">15.3.</strong> directive notes</a></li><li class="chapter-item "><a href="../../posts/angular/directive_communication.html"><strong aria-hidden="true">15.4.</strong> directive communication</a></li><li class="chapter-item "><a href="../../posts/angular/post_params.html"><strong aria-hidden="true">15.5.</strong> post params</a></li><li class="chapter-item "><a href="../../posts/angular/read_json_angular.html"><strong aria-hidden="true">15.6.</strong> read json angular</a></li><li class="chapter-item "><a href="../../posts/angular/same_route_reload.html"><strong aria-hidden="true">15.7.</strong> same route reload</a></li></ol></li><li class="chapter-item "><a href="../../posts/css/css.html"><strong aria-hidden="true">16.</strong> css</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/css/use_css_media.html"><strong aria-hidden="true">16.1.</strong> use css media</a></li></ol></li><li class="chapter-item "><a href="../../posts/php/php.html"><strong aria-hidden="true">17.</strong> php</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/php/for_php_string_implment_some_extemtion_functions.html"><strong aria-hidden="true">17.1.</strong> for php string implment some extemtion functions</a></li><li class="chapter-item "><a href="../../posts/php/php_cheatsheet.html"><strong aria-hidden="true">17.2.</strong> PHP cheatsheet</a></li></ol></li><li class="chapter-item "><a href="../../posts/leetcode/leetcode.html"><strong aria-hidden="true">18.</strong> leetcode</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/leetcode/rust_leetcode.html"><strong aria-hidden="true">18.1.</strong> rust leetcode</a></li><li class="chapter-item "><a href="../../posts/leetcode/rust_codewar.html"><strong aria-hidden="true">18.2.</strong> rust codewar</a></li><li class="chapter-item "><a href="../../posts/leetcode/swift_codewar.html"><strong aria-hidden="true">18.3.</strong> swift codewar</a></li><li class="chapter-item "><a href="../../posts/leetcode/js_leetcode.html"><strong aria-hidden="true">18.4.</strong> js leetcode</a></li><li class="chapter-item "><a href="../../posts/leetcode/java_leetcode.html"><strong aria-hidden="true">18.5.</strong> java leetcode</a></li><li class="chapter-item "><a href="../../posts/leetcode/rust_huawei.html"><strong aria-hidden="true">18.6.</strong> huawei test</a></li><li class="chapter-item "><a href="../../posts/leetcode/rust_utils.html"><strong aria-hidden="true">18.7.</strong> rust common functions</a></li><li class="chapter-item "><a href="../../posts/leetcode/olympiad_training.html"><strong aria-hidden="true">18.8.</strong> Computer olympiad training</a></li></ol></li><li class="chapter-item "><a href="../../posts/ctf/CTF.html"><strong aria-hidden="true">19.</strong> ctf</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/ctf/CTF_Note.html"><strong aria-hidden="true">19.1.</strong> CTF Note</a></li><li class="chapter-item "><a href="../../posts/ctf/0.1_Web.html"><strong aria-hidden="true">19.2.</strong> Web</a></li><li class="chapter-item "><a href="../../posts/ctf/4.1_Misc.html"><strong aria-hidden="true">19.3.</strong> Misc</a></li><li class="chapter-item "><a href="../../posts/ctf/3.2_PWN_note.html"><strong aria-hidden="true">19.4.</strong> PWN</a></li><li class="chapter-item "><a href="../../posts/ctf/3.1_Crypto.html"><strong aria-hidden="true">19.5.</strong> Crypto</a></li><li class="chapter-item "><a href="../../posts/ctf/3.4_RSA_note.html"><strong aria-hidden="true">19.6.</strong> Rsa attack</a></li><li class="chapter-item "><a href="../../posts/ctf/3.5_Base64.html"><strong aria-hidden="true">19.7.</strong> Base64</a></li><li class="chapter-item "><a href="../../posts/ctf/0.0_SQL Injection Cheatsheet.html"><strong aria-hidden="true">19.8.</strong> SQL Injection Cheatsheet</a></li><li class="chapter-item "><a href="../../posts/ctf/1.1_SQL_injection.html"><strong aria-hidden="true">19.9.</strong> SQL Injection</a></li><li class="chapter-item "><a href="../../posts/ctf/1.2_SQL_injection_UNION_attacks.html"><strong aria-hidden="true">19.10.</strong> SQL Injection UNION attacks</a></li><li class="chapter-item "><a href="../../posts/ctf/1.3_Blind SQL injection.html"><strong aria-hidden="true">19.11.</strong> Blind SQL Injection</a></li><li class="chapter-item "><a href="../../posts/ctf/1.4_Code Injection.html"><strong aria-hidden="true">19.12.</strong> Code Injection</a></li><li class="chapter-item "><a href="../../posts/ctf/1.5_SSRF.html"><strong aria-hidden="true">19.13.</strong> SSRF</a></li><li class="chapter-item "><a href="../../posts/ctf/1.6_OS command injection.html"><strong aria-hidden="true">19.14.</strong> OS command injection</a></li><li class="chapter-item "><a href="../../posts/ctf/1.7_Local file inclusion.html"><strong aria-hidden="true">19.15.</strong> Local file inclusion</a></li><li class="chapter-item "><a href="../../posts/ctf/1.8_Remote file inclusion.html"><strong aria-hidden="true">19.16.</strong> Remote file inclusion</a></li><li class="chapter-item "><a href="../../posts/ctf/1.9_CSRFm.html"><strong aria-hidden="true">19.17.</strong> CSRF</a></li><li class="chapter-item "><a href="../../posts/ctf/1.10_NoSQL injection.html"><strong aria-hidden="true">19.18.</strong> NoSQL injection</a></li><li class="chapter-item "><a href="../../posts/ctf/1.11_JSON injection.html"><strong aria-hidden="true">19.19.</strong> JSON injection</a></li><li class="chapter-item "><a href="../../posts/ctf/1.12_CTF_Web_SQL_Note.html"><strong aria-hidden="true">19.20.</strong> CTF Web SQL Note</a></li><li class="chapter-item "><a href="../../posts/ctf/2.1_XXE.html"><strong aria-hidden="true">19.21.</strong> XXE</a></li><li class="chapter-item "><a href="../../posts/ctf/2.2_XSS.html"><strong aria-hidden="true">19.22.</strong> XSS</a></li><li class="chapter-item "><a href="../../posts/ctf/2.3_Upload File.html"><strong aria-hidden="true">19.23.</strong> Upload File</a></li><li class="chapter-item "><a href="../../posts/ctf/2.4_serialize_unserialize.html"><strong aria-hidden="true">19.24.</strong> serialize unserialize</a></li><li class="chapter-item "><a href="../../posts/ctf/2.5_Race condition.html"><strong aria-hidden="true">19.25.</strong> Race condition</a></li><li class="chapter-item "><a href="../../posts/ctf/3.2_PWN_note.html"><strong aria-hidden="true">19.26.</strong> PWN_note</a></li><li class="chapter-item "><a href="../../posts/ctf/3.3_pwn HCTF2016 brop.html"><strong aria-hidden="true">19.27.</strong> pwn HCTF2016 brop</a></li><li class="chapter-item "><a href="../../posts/ctf/pwn_patch_defense_skill.html"><strong aria-hidden="true">19.28.</strong> PWN Patch defense skill</a></li><li class="chapter-item "><a href="../../posts/ctf/pwn_stack_overflow.html"><strong aria-hidden="true">19.29.</strong> PWN stack overflow</a></li><li class="chapter-item "><a href="../../posts/ctf/pwn_heap_overflow.html"><strong aria-hidden="true">19.30.</strong> PWN heap overflow</a></li><li class="chapter-item "><a href="../../posts/ctf/pwn_format_string_vulnerability.html"><strong aria-hidden="true">19.31.</strong> PWN Format String Vulnerability</a></li><li class="chapter-item "><a href="../../posts/ctf/kali_linux_tutorials.html"><strong aria-hidden="true">19.32.</strong> Kali linux tutorials</a></li><li class="chapter-item "><a href="../../posts/ctf/google_dorks_2023_lists.html"><strong aria-hidden="true">19.33.</strong> Google Dorks 2023 Lists</a></li><li class="chapter-item "><a href="../../posts/ctf/dvwa_writeup.html"><strong aria-hidden="true">19.34.</strong> DVWA WriteUp</a></li><li class="chapter-item "><a href="../../posts/ctf/bwapp_writeup.html"><strong aria-hidden="true">19.35.</strong> bWAPP WriteUp</a></li><li class="chapter-item "><a href="../../posts/ctf/sqlilabs_writeup.html"><strong aria-hidden="true">19.36.</strong> sqlilabs WriteUp</a></li><li class="chapter-item "><a href="../../posts/ctf/ctf_train_at_hangzhou.html"><strong aria-hidden="true">19.37.</strong> ctf train at hangzhou</a></li><li class="chapter-item "><a href="../../posts/ctf/ctf_common_mindmap_list.html"><strong aria-hidden="true">19.38.</strong> ctf common mindmap list</a></li><li class="chapter-item "><a href="../../posts/ctf/error_based_sql_injection.html"><strong aria-hidden="true">19.39.</strong> Error Based SQL Injection</a></li><li class="chapter-item "><a href="../../posts/ctf/urlfinder_tutorial.html"><strong aria-hidden="true">19.40.</strong> URLFinder Tutorial</a></li><li class="chapter-item "><a href="../../posts/ctf/observer_ward_tutorial.html"><strong aria-hidden="true">19.41.</strong> observer_ward Tutorial</a></li><li class="chapter-item "><a href="../../posts/ctf/mysql_udf_.html"><strong aria-hidden="true">19.42.</strong> MySQL UDF 提权</a></li><li class="chapter-item "><a href="../../posts/ctf/nuclei__tutorial.html"><strong aria-hidden="true">19.43.</strong> Nuclei Tutorial</a></li><li class="chapter-item "><a href="../../posts/ctf/2024_ctf_solution_thinking.html"><strong aria-hidden="true">19.44.</strong> 2024 ctf solution thinking</a></li><li class="chapter-item "><a href="../../posts/ctf/man_che_si_te_bian_ma.html"><strong aria-hidden="true">19.45.</strong> 曼彻斯特编码</a></li></ol></li></ol>
|
||
</div>
|
||
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
|
||
<div class="sidebar-resize-indicator"></div>
|
||
</div>
|
||
</nav>
|
||
|
||
<!-- Track and set sidebar scroll position -->
|
||
<script>
|
||
var sidebarScrollbox = document.querySelector('#sidebar .sidebar-scrollbox');
|
||
sidebarScrollbox.addEventListener('click', function(e) {
|
||
if (e.target.tagName === 'A') {
|
||
sessionStorage.setItem('sidebar-scroll', sidebarScrollbox.scrollTop);
|
||
}
|
||
}, { passive: true });
|
||
var sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
|
||
sessionStorage.removeItem('sidebar-scroll');
|
||
if (sidebarScrollTop) {
|
||
// preserve sidebar scroll position when navigating via links within sidebar
|
||
sidebarScrollbox.scrollTop = sidebarScrollTop;
|
||
} else {
|
||
// scroll sidebar to current active section when navigating via "next/previous chapter" buttons
|
||
var activeSection = document.querySelector('#sidebar .active');
|
||
if (activeSection) {
|
||
activeSection.scrollIntoView({ block: 'center' });
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<div id="page-wrapper" class="page-wrapper">
|
||
|
||
<div class="page">
|
||
<div id="menu-bar-hover-placeholder"></div>
|
||
<div id="menu-bar" class="menu-bar sticky">
|
||
<div class="left-buttons">
|
||
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
||
<i class="fa fa-bars"></i>
|
||
</label>
|
||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
||
<i class="fa fa-paint-brush"></i>
|
||
</button>
|
||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
||
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
|
||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
||
</ul>
|
||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
||
<i class="fa fa-search"></i>
|
||
</button>
|
||
</div>
|
||
|
||
<h1 class="menu-title">Andrew's Blog</h1>
|
||
|
||
<div class="right-buttons">
|
||
<a href="https://gitlink.org.cn/dnrops/dnrops.gitlink.net.git" title="Git repository" aria-label="Git repository">
|
||
<i id="git-repository-button" class="fa fa-github"></i>
|
||
</a>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
<div id="search-wrapper" class="hidden">
|
||
<form id="searchbar-outer" class="searchbar-outer">
|
||
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
||
</form>
|
||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
||
<div id="searchresults-header" class="searchresults-header"></div>
|
||
<ul id="searchresults">
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
||
<script>
|
||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
||
});
|
||
</script>
|
||
|
||
<div id="content" class="content">
|
||
<main>
|
||
<h1 id="java-grammar--java-codewar-solutions"><a class="header" href="#java-grammar--java-codewar-solutions">Java grammar && Java Codewar Solutions</a></h1>
|
||
<h2 id="java-多个线程同步运行"><a class="header" href="#java-多个线程同步运行">Java 多个线程同步运行</a></h2>
|
||
<pre><code class="language-java">class Counter{
|
||
int count;
|
||
public synchronized void increment(){ //两个线程不能同步执行该方法 加 synchronized
|
||
count++;
|
||
}
|
||
}
|
||
class RunSyncThread{
|
||
public void run() throws Exception{
|
||
Counter c = new Counter();
|
||
Thread t1 = new Thread(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
for (int i = 0;i<1000;i++){
|
||
c.increment();
|
||
}
|
||
}
|
||
});
|
||
Thread t2 = new Thread(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
for (int i = 0;i<1000;i++){
|
||
c.increment();
|
||
}
|
||
}
|
||
});
|
||
t1.run();
|
||
t2.run();
|
||
t1.join();
|
||
t2.join();
|
||
System.out.println(c.count);
|
||
}
|
||
}
|
||
public class kata {
|
||
public static void main(String[] args) throws Exception {
|
||
RunSyncThread run_sync_thread = new RunSyncThread();
|
||
run_sync_thread.run();
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="java-streams"><a class="header" href="#java-streams">Java Streams</a></h2>
|
||
<pre><code class="language-java">import java.util.List;
|
||
import java.util.Set;
|
||
import java.util.Objects;
|
||
import java.util.stream.Collectors;
|
||
import static Streams._Stream.Gender.*;
|
||
public class _Stream {
|
||
public static void main(String[] args) {
|
||
List<_Stream.Person> people = List.of(
|
||
new _Stream.Person("Alen", MALE),
|
||
new _Stream.Person("Andrey", MALE),
|
||
new _Stream.Person("Alex", FEMALE),
|
||
new _Stream.Person("Amili", FEMALE),
|
||
new _Stream.Person("Mali", PREFER_NOT_TO_SAY)
|
||
);
|
||
// [MALE, FEMALE]
|
||
Set<Gender> genders = people.stream()
|
||
.map(person -> person.gender)
|
||
.collect(Collectors.toSet());
|
||
System.out.println(genders);
|
||
// [Anana, Alex, Andrey, Alen, Amili]
|
||
Set<String> names = people.stream()
|
||
.map(person -> person.name)
|
||
.collect(Collectors.toSet());
|
||
System.out.println(names);
|
||
// filter some people in List
|
||
List<Person> the_people_of_DONT_WANT_TO_SAY = people.stream().filter(ITEM ->ITEM.gender.equals(PREFER_NOT_TO_SAY)).collect(Collectors.toList());
|
||
System.out.println("the_people_of_DONT_WANT_TO_SAY: "+the_people_of_DONT_WANT_TO_SAY);
|
||
System.out.println("the_people_of_DONT_WANT_TO_SAY COUNT : "+people.stream().filter(ITEM ->ITEM.gender.equals(PREFER_NOT_TO_SAY)).count()); //count 满足条件的元素
|
||
// Determine the presence or absence
|
||
// .anyMatch(ITEM->ITEM.gender.equals(PREFER_NOT_TO_SAY)); //存在一个 就返回true
|
||
// .allMatch(ITEM->ITEM.gender.equals(PREFER_NOT_TO_SAY)); //所有都满足 就返回true else 返回false
|
||
// .noneMatch(ITEM->ITEM.gender.equals(PREFER_NOT_TO_SAY)); //与allMatch相反,判断条件里的元素,所有的都不是,返回true
|
||
Boolean presence_SOME_PEOPLE_DONT_WANT_TO_SAY = people.stream().anyMatch(ITEM->ITEM.gender.equals(PREFER_NOT_TO_SAY)); //存在一个 就返回true
|
||
System.out.println("presence_SOME_PEOPLE_DONT_WANT_TO_SAY: "+presence_SOME_PEOPLE_DONT_WANT_TO_SAY);
|
||
}
|
||
record Person(String name, Gender gender) {
|
||
@Override
|
||
public String toString() {
|
||
return "Person{" +
|
||
"name='" + name + '\'' +
|
||
", gender=" + gender +
|
||
'}';
|
||
}
|
||
@Override
|
||
public int hashCode() {
|
||
return Objects.hash(name, gender);
|
||
}
|
||
}
|
||
enum Gender{
|
||
MALE,FEMALE,PREFER_NOT_TO_SAY,
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="java-stringbuffer"><a class="header" href="#java-stringbuffer">Java StringBuffer</a></h2>
|
||
<pre><code class="language-java">public class _StringBuffer {
|
||
public static void main(String[] args) {
|
||
StringBuffer sb = new StringBuffer();
|
||
sb.append(15); //add int
|
||
sb.append("string");
|
||
sb.append(true);
|
||
sb.append(15);
|
||
sb.append(15);
|
||
sb.append(15);
|
||
sb.append(15);
|
||
sb.append(15);
|
||
sb.insert(0, "first "); //0 index insert value
|
||
sb.reverse(); //反转所有字符
|
||
sb.delete(0, 5); //delete index 0..=4
|
||
sb.replace(5, 8, "are"); //replace index 5..=7
|
||
System.out.println("Print StingBuffer : "+sb); //convert to string
|
||
System.out.println(sb.charAt(0)); //get 0 index item
|
||
System.out.println(sb.length()); //length
|
||
System.out.println(sb.indexOf("string")); //get string index
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="java-math"><a class="header" href="#java-math">Java Math</a></h2>
|
||
<pre><code class="language-java">public class _Math {
|
||
public static void main(String[] args) {
|
||
// print the absolute value
|
||
System.out.println(Math.abs(-7.89));
|
||
// create long variable
|
||
long c = 12345678l;
|
||
long d = 987654321l;
|
||
// addExact() with long arguments
|
||
System.out.println(Math.addExact(c, d)); // 999999999
|
||
// compute square root of 25
|
||
System.out.println(Math.sqrt(25));
|
||
// create a double variable
|
||
double value2 = 27.0; //三次根号
|
||
System.out.println(Math.cbrt(value2)); // 3.0
|
||
// computes 5 raised to the power 3
|
||
System.out.println(Math.pow(5, 3));
|
||
// Math.min() with float arguments
|
||
float num5 = 4.5f;
|
||
float num6 = 9.67f;
|
||
System.out.println(Math.min(num5, num6)); // 4.5
|
||
// Math.min() with double arguments
|
||
double num7 = 23.44d;
|
||
double num8 = 32.11d;
|
||
System.out.println(Math.min(num7, num8)); // 23.44
|
||
// compute max of 88 and 98
|
||
System.out.println(Math.max(88, 98));
|
||
// Math.ceil() method (1.0 大 返回2.0)
|
||
// value greater than 5 after decimal
|
||
double a = 1.878;
|
||
System.out.println(Math.ceil(a)); // 2.0
|
||
// value equals to 5 after decimal
|
||
double b = 1.5;
|
||
System.out.println(Math.ceil(b)); // 2.0
|
||
// value less than 5 after decimal
|
||
double c1 = 1.34;
|
||
System.out.println(Math.ceil(c1)); // 2.0
|
||
//Math.floor() 向下取整
|
||
double a1 = 3.8;
|
||
System.out.println(Math.floor(a1)); //3
|
||
//Math.round() 向上取整
|
||
double a2 = 3.8;
|
||
System.out.println(Math.round(a2)); //4
|
||
//Ganerate Random number between 10 and 20
|
||
int upperBound = 20;
|
||
int lowerBound = 10;
|
||
// upperBound 20 will also be included
|
||
int range = (upperBound - lowerBound) + 1;
|
||
System.out.println("Random Numbers between 10 and 20:");
|
||
for (int i = 0; i < 10; i ++) {
|
||
// generate random number
|
||
// (int) convert double value to int
|
||
// Math.round() generate value between 0.0 and 1.0
|
||
int random = (int)(Math.random() * range) + lowerBound;
|
||
System.out.print(random + ", ");
|
||
}
|
||
// Math.rint()
|
||
// value greater than 5 after decimal
|
||
System.out.println(Math.rint(1.878)); // 2.0
|
||
// value less than 5 after decimal
|
||
System.out.println(Math.rint(1.34)); // 1.0
|
||
// value equal to 5 after decimal
|
||
System.out.println(Math.rint(1.5)); // 2.0
|
||
// value equal to 5 after decimal
|
||
System.out.println(Math.rint(2.5)); // 2.0
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="java-read-file"><a class="header" href="#java-read-file">Java Read File</a></h2>
|
||
<pre><code class="language-java">import java.io.*;
|
||
import java.nio.charset.StandardCharsets;
|
||
import java.nio.file.Files;
|
||
import java.nio.file.Paths;
|
||
import java.util.Collections;
|
||
import java.util.Iterator;
|
||
import java.util.List;
|
||
import java.util.Scanner;
|
||
public class _Read_File {
|
||
public static void ReadFile_Use_BufferedReader() throws IOException {
|
||
// We need to provide file path as the parameter:
|
||
// double back quote is to avoid compiler interpret words
|
||
// like \test as \t (ie as escape sequence)
|
||
File file = new File("/home/alen/code/java_code/learn/src/File/test.txt");
|
||
BufferedReader br = new BufferedReader(new FileReader(file));
|
||
String st;
|
||
while ((st = br.readLine()) != null) {
|
||
System.out.println(st);
|
||
}
|
||
}
|
||
public static void ReadFile_Use_FileReader() throws IOException {
|
||
// pass the path to the file as a parameter
|
||
FileReader fr =
|
||
new FileReader("/home/alen/code/java_code/learn/src/File/test.txt");
|
||
int i;
|
||
while ((i=fr.read()) != -1)
|
||
System.out.print((char) i);
|
||
}
|
||
public static void ReadFile_Use_Scanner() throws IOException {
|
||
// pass the path to the file as a parameter
|
||
{
|
||
File file =
|
||
new File("/home/alen/code/java_code/learn/src/File/test.txt");
|
||
Scanner sc = new Scanner(file);
|
||
while (sc.hasNextLine())
|
||
System.out.println(sc.nextLine());
|
||
}
|
||
{
|
||
//without loop
|
||
File file = new File("/home/alen/code/java_code/learn/src/File/test.txt");
|
||
Scanner sc = new Scanner(file);
|
||
// we just need to use \\Z as delimiter
|
||
sc.useDelimiter("\\Z");
|
||
System.out.println(sc.next());
|
||
}
|
||
}
|
||
public static List<String> readFileInList(String fileName)
|
||
{
|
||
List<String> lines = Collections.emptyList();
|
||
try
|
||
{
|
||
lines =
|
||
Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
|
||
}
|
||
catch (IOException e)
|
||
{
|
||
// do something
|
||
e.printStackTrace();
|
||
}
|
||
return lines;
|
||
}
|
||
public static void ReadFileIntoList(){
|
||
List l = readFileInList("/home/alen/code/java_code/learn/src/File/test.txt");
|
||
Iterator<String> itr = l.iterator();
|
||
while (itr.hasNext())
|
||
System.out.println(itr.next());
|
||
}
|
||
public static String readFileAsString(String fileName)throws Exception
|
||
{
|
||
String data;
|
||
data = new String(Files.readAllBytes(Paths.get(fileName)));
|
||
return data;
|
||
}
|
||
public static void main(String[] args) throws Exception {
|
||
System.out.println("ReadFile_Use_BufferedReader:");
|
||
ReadFile_Use_BufferedReader();
|
||
System.out.println("ReadFile_Use_FileReader:");
|
||
ReadFile_Use_FileReader();
|
||
System.out.println("ReadFile_Use_Scanner:");
|
||
ReadFile_Use_Scanner();
|
||
System.out.println("ReadFileIntoList:");
|
||
ReadFileIntoList();
|
||
System.out.println("readFileAsString:");
|
||
String data = readFileAsString("/home/alen/code/java_code/learn/src/File/test.js");
|
||
System.out.println(data);
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="java-write-file"><a class="header" href="#java-write-file">Java Write File</a></h2>
|
||
<pre><code class="language-java">import java.io.*;
|
||
public class _Write_File {
|
||
public static void Write_File_Use_BufferedWriter(){
|
||
try {
|
||
BufferedWriter out = new BufferedWriter(new FileWriter("/home/alen/code/java_code/learn/src/File/wrote_file.txt"));
|
||
out.write("a String");
|
||
out.close();
|
||
System.out.println("File created successfully");
|
||
}
|
||
catch (IOException e) {
|
||
}
|
||
}
|
||
public static void main(String[] args) {
|
||
Write_File_Use_BufferedWriter();
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="java-run-command"><a class="header" href="#java-run-command">Java Run Command</a></h2>
|
||
<pre><code class="language-java">import java.io.BufferedReader;
|
||
import java.io.IOException;
|
||
import java.io.InputStreamReader;
|
||
public class _Run_py {
|
||
public static void run_py(){
|
||
try {
|
||
Process p = Runtime.getRuntime().exec("python /home/alen/code/java_code/learn/src/exec/test.py");
|
||
BufferedReader in = new BufferedReader(
|
||
new InputStreamReader(p.getInputStream()));
|
||
String line = null;
|
||
while ((line = in.readLine()) != null) {
|
||
System.out.println(line);
|
||
}
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
public static void run_ls(){
|
||
try {
|
||
Process p = Runtime.getRuntime().exec("ls -lha");
|
||
BufferedReader in = new BufferedReader(
|
||
new InputStreamReader(p.getInputStream()));
|
||
String line = null;
|
||
while ((line = in.readLine()) != null) {
|
||
System.out.println(line);
|
||
}
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
public static void main(String[] args) throws IOException {
|
||
run_py();
|
||
run_ls();
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="java-callback"><a class="header" href="#java-callback">Java CallBack</a></h2>
|
||
<pre><code class="language-java">import java.util.function.Consumer;
|
||
public class _CallBack {
|
||
public static void main(String[] args) {
|
||
hello("Alen",null,value->System.out.println("use Consumer: last_name not defined for :"+value));
|
||
hello1("Alen",null,()->System.out.println(" use Runnable: last_name not defined"));
|
||
}
|
||
// 用Consumer 跟javascript 的callback 一样 使用 callback
|
||
static void hello(String fist_name, String lat_name, Consumer<String> callback){
|
||
System.out.println(fist_name);
|
||
if (lat_name !=null){
|
||
System.out.println(lat_name);
|
||
}else {
|
||
callback.accept(fist_name);
|
||
}
|
||
}
|
||
// 用Runnable 跟javascript 的callback 一样 使用 callback
|
||
static void hello1(String fist_name, String lat_name, Runnable callback){
|
||
System.out.println(fist_name);
|
||
if (lat_name !=null){
|
||
System.out.println(lat_name);
|
||
}else {
|
||
callback.run();
|
||
}
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="java-consumer"><a class="header" href="#java-consumer">Java Consumer</a></h2>
|
||
<pre><code class="language-java">package FunctionalInterface;
|
||
import java.util.function.BiConsumer;//take two argument return void
|
||
import java.util.function.Consumer;//take one argument return void
|
||
public class _Consumer {
|
||
public static void main(String[] args) {
|
||
System.out.println("// normal function");
|
||
greetingCustomer(new Customer("Alen", "15354843621"));
|
||
System.out.println("// Customer Function");
|
||
greetingCustomer_ByConsumer.accept(new Customer("Andry", "19909284728"));
|
||
System.out.println("// BiCustomer Function");
|
||
greetingCustomer_ByBiConsumer.accept((new Customer("Andry", "19909284728")),false);
|
||
}
|
||
// use Consumer
|
||
static Consumer<Customer> greetingCustomer_ByConsumer = consumer->
|
||
System.out.println("Hello "+consumer.CustomerName+" Thanks for registering phone number "+consumer.CustomerPhoneNumber);
|
||
// use void function
|
||
static void greetingCustomer(Customer customer){
|
||
System.out.println("Hello "+customer.CustomerName+" Thanks for registering phone number "+customer.CustomerPhoneNumber);
|
||
}
|
||
// BiConsumer
|
||
static BiConsumer<Customer,Boolean> greetingCustomer_ByBiConsumer = (customer,showPhoneNumber) ->
|
||
System.out.println("Hello "+customer.CustomerName+" Thanks for registering phone number "+ (showPhoneNumber ? customer.CustomerPhoneNumber:"***************"));
|
||
static class Customer{
|
||
private final String CustomerName;
|
||
private final String CustomerPhoneNumber;
|
||
public Customer(String customerName, String customerPhoneNumber) {
|
||
CustomerName = customerName;
|
||
CustomerPhoneNumber = customerPhoneNumber;
|
||
}
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="java-function"><a class="header" href="#java-function">Java Function</a></h2>
|
||
<pre><code class="language-java">package FunctionalInterface;
|
||
import java.util.function.BiFunction; //take two argument return one
|
||
import java.util.function.Function; //take one argument return one
|
||
public class _Function {
|
||
public static void main(String[] args) {
|
||
System.out.println("***function***");
|
||
int inc = increment(0);
|
||
System.out.println(inc);
|
||
System.out.println("***Function***");
|
||
int inc1 = incrementFunction.apply(0);
|
||
System.out.println(inc1);
|
||
//合并两个Function
|
||
Function<Integer,Integer> mergeFunction = incrementFunction.andThen(multipleBy10);
|
||
int mergeResult = mergeFunction.apply(0);
|
||
System.out.println(mergeResult);
|
||
System.out.println(incrementFunction.andThen(multipleBy10).apply(0));
|
||
//BIFunction
|
||
System.out.println("***BiFunction***");
|
||
BiFunction<Integer,Integer,Integer> multipleBiFunction = (num,numToMultipleBy) //2个输入参数 1个输出参数
|
||
->(num +1)*numToMultipleBy;
|
||
System.out.println(multipleBiFunction.apply(4,100));
|
||
}
|
||
// function
|
||
static int increment(int num){
|
||
num++;
|
||
return num;
|
||
}
|
||
// Function
|
||
static Function<Integer,Integer> incrementFunction = num -> num+1;
|
||
static Function<Integer,Integer> multipleBy10 = num -> num*10;
|
||
}
|
||
</code></pre>
|
||
<h2 id="java-predicate"><a class="header" href="#java-predicate">Java Predicate</a></h2>
|
||
<pre><code class="language-java">package FunctionalInterface;
|
||
import java.util.function.BiPredicate; //take Two argument return Boolean
|
||
import java.util.function.Predicate;//take one argument return Boolean
|
||
public class _Predicate {
|
||
public static void main(String[] args) {
|
||
System.out.println(isPhoneNumberValid("15354843621"));
|
||
System.out.println(isPhoneNumberValid("153548436201"));
|
||
System.out.println(isPhoneNumberValid_ByUse_Predicate.test("15354843621"));
|
||
// 合并组合使用Predicate and 同时满足为true
|
||
System.out.println("// Predicate");
|
||
Boolean result = isPhoneNumberValid_ByUse_Predicate.and(containsNumber).test("15354843621");
|
||
System.out.println(result);
|
||
// 合并组合使用Predicate 和 BiPredicate && 同时满足为true
|
||
System.out.println("// BiPredicate");
|
||
Boolean result1 = (isPhoneNumberValid_ByUse_Predicate.test("15354843621") &&containsNumber_Use_BiPredicate.test("15354843621",'9') );
|
||
System.out.println(result1);
|
||
}
|
||
static boolean isPhoneNumberValid(String phoneNumber){
|
||
return phoneNumber.startsWith("1") && phoneNumber.length() ==11;
|
||
}
|
||
static Predicate<String> isPhoneNumberValid_ByUse_Predicate = phoneNumber->
|
||
phoneNumber.startsWith("1") && phoneNumber.length() ==11;
|
||
static Predicate<String> containsNumber = phoneNumber ->
|
||
phoneNumber.contains("3");
|
||
static BiPredicate<String,Character> containsNumber_Use_BiPredicate = (phoneNumber,num) ->
|
||
phoneNumber.contains(num.toString());
|
||
}
|
||
</code></pre>
|
||
<h2 id="java-supplier"><a class="header" href="#java-supplier">Java Supplier</a></h2>
|
||
<pre><code class="language-java">package FunctionalInterface;
|
||
import java.util.List;
|
||
import java.util.function.Supplier;//do not take any argument
|
||
public class _Supplier {
|
||
public static void main(String[] args) {
|
||
System.out.println("normal function");
|
||
System.out.println(getConnectionUrl());
|
||
System.out.println("use Supplier ");
|
||
System.out.println(getConnectionUrl_Use_Supplier.get());
|
||
}
|
||
static String getConnectionUrl(){
|
||
return "jdbc://localhost5432/user";
|
||
}
|
||
static Supplier<List<String>> getConnectionUrl_Use_Supplier = ()->
|
||
List.of("jdbc://localhost5432/user",
|
||
"jdbc://localhost5432/user",
|
||
"jdbc://localhost5432/user"
|
||
);
|
||
}
|
||
</code></pre>
|
||
<h2 id="kata1-用ascii码加密-数据"><a class="header" href="#kata1-用ascii码加密-数据">Kata1 (用ASCII码加密 数据)</a></h2>
|
||
<pre><code class="language-java">/*
|
||
*Encrypt this!
|
||
*Examples:
|
||
Kata.encryptThis("Hello") => "72olle"
|
||
Kata.encryptThis("good") => "103doo"
|
||
Kata.encryptThis("hello world") => "104olle 119drlo"
|
||
*/
|
||
import java.util.*;
|
||
import java.util.stream.Collectors;
|
||
import java.util.function.*;
|
||
class Kata1 {
|
||
public static String encryptThis(String text) {
|
||
UnaryOperator<String> encrtyper = s -> {
|
||
switch (s.length()) {
|
||
case 0:
|
||
return s;
|
||
case 1:
|
||
return String.valueOf((int) s.charAt(0));
|
||
case 2:
|
||
return (int) s.charAt(0) + s.substring(s.length()-1);
|
||
default:
|
||
return (int) s.charAt(0) + s.substring(s.length()-1) + s.substring(2, s.length()-1) + s.substring(1, 2);
|
||
}
|
||
};
|
||
return Arrays.stream(text.split(" "))
|
||
.map(s -> encrtyper.apply(s))
|
||
.collect(Collectors.joining(" "));
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="kata2-转化为二进制-数1的个数"><a class="header" href="#kata2-转化为二进制-数1的个数">Kata2 (转化为二进制 ,数1的个数)</a></h2>
|
||
<pre><code class="language-java">/***
|
||
* Bit Counting (转化为二进制 ,数1的个数)
|
||
* Example: The binary representation of 1234 is 10011010010, so the function should return 5 in this case
|
||
*/
|
||
import java.util.*;
|
||
import java.util.stream.Collectors;
|
||
import java.util.function.*;
|
||
class BitCounting1 {
|
||
public static int countBits(int n){
|
||
// Show me the code!
|
||
String binary = String.valueOf(Integer.toBinaryString(n));
|
||
int count = 0;
|
||
for(String s : binary.split("")){
|
||
if(s.equals("1")){
|
||
count++;
|
||
}
|
||
}
|
||
return count;
|
||
}
|
||
}
|
||
class BitCounting2 {
|
||
public static int countBits(int n){
|
||
return Integer.bitCount(n);
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="kata3-返回最小和最大数"><a class="header" href="#kata3-返回最小和最大数">Kata3 (返回,最小和最大数)</a></h2>
|
||
<pre><code class="language-java">/***
|
||
* The highest profit wins!(返回,最小和最大数)
|
||
* Examples
|
||
* MinMax.minMax(new int[]{1,2,3,4,5}) == {1,5}
|
||
* MinMax.minMax(new int[]{2334454,5}) == {5, 2334454}
|
||
* MinMax.minMax(new int[]{1}) == {1, 1}
|
||
*/
|
||
import java.util.*;
|
||
import java.util.stream.Collectors;
|
||
import java.util.function.*;
|
||
class MinMax {
|
||
public static int[] minMax(int[] arr) {
|
||
// Your awesome code here
|
||
int min = Arrays.stream(arr)
|
||
.min()
|
||
.getAsInt();
|
||
int max = Arrays.stream(arr)
|
||
.max()
|
||
.getAsInt();
|
||
return new int[]{min, max};
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="kata4-排序array"><a class="header" href="#kata4-排序array">Kata4 (排序array)</a></h2>
|
||
<pre><code class="language-java">/***
|
||
* Array Sort (数组小到大排序) int
|
||
*/
|
||
import java.util.*;
|
||
import java.util.stream.Collectors;
|
||
import java.util.function.*;
|
||
class Sort1{
|
||
public static int[] ArraySort(int[] arr){
|
||
Arrays.sort(arr);
|
||
return arr;
|
||
}
|
||
//System.out.println(Arrays.toString(Sort.ArraySort(new int[]{51,26,6,14,2}))); //[2, 6, 14, 26, 51]
|
||
}
|
||
/***
|
||
* Array Sort (数组小到大排序) String
|
||
*/
|
||
class Sort2{
|
||
public static String[] ArraySort(String[] arr){
|
||
Arrays.sort(arr);
|
||
return arr;
|
||
}
|
||
// System.out.println(Arrays.toString(Sort2.ArraySort(new String[]{"d","a","c"}))); //[a, c, d]
|
||
}
|
||
</code></pre>
|
||
<h2 id="kata5-卡号部分模糊显示"><a class="header" href="#kata5-卡号部分模糊显示">Kata5 (卡号部分模糊显示)</a></h2>
|
||
<pre><code class="language-java">/**
|
||
*Credit Card Mask (卡号模糊显示)
|
||
* Examples
|
||
* Maskify.Maskify("4556364607935616"); // should return "############5616"
|
||
* Maskify.Maskify("64607935616"); // should return "#######5616"
|
||
* Maskify.Maskify("1"); // should return "1"
|
||
* Maskify.Maskify(""); // should return ""
|
||
* // "What was the name of your first pet?"
|
||
* Maskify.Maskify("Skippy"); // should return "##ippy"
|
||
* Maskify.Maskify("Nananananananananananananananana Batman!"); // should return "####################################man!"
|
||
*/
|
||
import java.util.*;
|
||
import java.util.stream.Collectors;
|
||
import java.util.function.*;
|
||
class Maskify {
|
||
public static String maskify(String str) {
|
||
List<String> list = new ArrayList<String>();
|
||
for (String s : str.split("")){
|
||
list.add(s);
|
||
}
|
||
if(list.size()>4){
|
||
for (int i = 0; i < list.size()-4; i++) {
|
||
list.set(i,"*");
|
||
}
|
||
}
|
||
// System.out.println(list.stream().collect(Collectors.joining(" ")));
|
||
return list.stream().collect(Collectors.joining(""));
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="kata6-统计元素出现次数输出奇数次数的元素"><a class="header" href="#kata6-统计元素出现次数输出奇数次数的元素">Kata6 (统计元素出现次数,输出奇数次数的元素)</a></h2>
|
||
<pre><code class="language-java">import java.util.*;
|
||
import java.util.stream.Collectors;
|
||
import java.util.function.*;
|
||
class FindOdd {
|
||
public static int findIt(int[] a) {
|
||
HashMap<String, Integer> hashMap = new HashMap<>();
|
||
for (Integer s : a){
|
||
if(!hashMap.containsKey(s.toString())) {
|
||
hashMap.put(s.toString(), 0);
|
||
}
|
||
}
|
||
for (Integer s : a){
|
||
if(hashMap.containsKey(s.toString())) {
|
||
Integer value = hashMap.get(s.toString());
|
||
value = value+1;
|
||
hashMap.put(s.toString(),value);
|
||
}
|
||
}
|
||
// System.out.println(hashMap); //{1=2, 2=2, 3=2, 4=2, 5=3, 20=2}
|
||
// System.out.println(hashMap.values());
|
||
Integer oddIndex =0;
|
||
for(Integer s : hashMap.values()) {
|
||
oddIndex++;
|
||
if(s%2!=0){
|
||
// System.out.println(s);
|
||
break;
|
||
}
|
||
}
|
||
// System.out.println(hashMap.keySet()); //return key set
|
||
// System.out.println(hashMap.keySet().toArray()[oddIndex-1]);
|
||
// System.out.println(oddIndex);
|
||
// System.out.println(FindOdd.findIt(new int[]{2,2,3,4,55,55,55})); // 输出出现次数 {55=3, 2=2, 3=1, 4=1}
|
||
Integer oddNumber = Integer.parseInt((String) hashMap.keySet().toArray()[oddIndex-1]);
|
||
return oddNumber;
|
||
}
|
||
}
|
||
//solution 2
|
||
class FindOdd1 {
|
||
public static int findIt(int[] A) {
|
||
final TreeSet<Integer> set = new TreeSet<>();
|
||
for (int x : A) {
|
||
if (set.contains(x)) {
|
||
set.remove(x); //because return odd frequency so remove even frequency
|
||
} else {
|
||
set.add(x);
|
||
}
|
||
}
|
||
// System.out.println(set); //[5]
|
||
return set.first();
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="kata7-输出子字符串"><a class="header" href="#kata7-输出子字符串">Kata7 (输出子字符串)</a></h2>
|
||
<pre><code class="language-java">/***
|
||
*Which are in?
|
||
*Given two arrays of strings a1 and a2 return a sorted array r in lexicographical order of the strings of a1 which are substrings of strings of a2.
|
||
*
|
||
* Example 1:
|
||
* a1 = ["arp", "live", "strong"]
|
||
*
|
||
* a2 = ["lively", "alive", "harp", "sharp", "armstrong"]
|
||
*
|
||
* returns ["arp", "live", "strong"]
|
||
*/
|
||
import java.util.*;
|
||
import java.util.stream.Collectors;
|
||
import java.util.function.*;
|
||
class WhichAreIn {
|
||
public static String[] inArray(String[] array1, String[] array2) {
|
||
return Arrays.stream(array1)
|
||
.filter(str -> Arrays.stream(array2).anyMatch(s -> s.contains(str)))
|
||
.distinct()
|
||
.sorted()
|
||
.toArray(String[]::new);
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="kata8-判断出现ox次数是否相同"><a class="header" href="#kata8-判断出现ox次数是否相同">Kata8 (判断出现o,x次数是否相同)</a></h2>
|
||
<pre><code class="language-java">/***
|
||
*Exes and Ohs (if O和X的出现次数相同=>true)
|
||
* Examples input/output:
|
||
*
|
||
* XO("ooxx") => true
|
||
* XO("xooxx") => false
|
||
* XO("ooxXm") => true
|
||
* XO("zpzpzpp") => true // when no 'x' and 'o' is present should return true
|
||
* XO("zzoo") => false
|
||
*/
|
||
class XO {
|
||
public static boolean getXO (String str) {
|
||
str = str.toLowerCase(Locale.ROOT); //转化为小写
|
||
HashMap<String, Integer> hashMap = new HashMap<>();
|
||
for (String s : str.split("")){
|
||
if(!hashMap.containsKey(s.toString())) {
|
||
hashMap.put(s, 0);
|
||
}
|
||
}
|
||
for (String s : str.split("")){
|
||
if(hashMap.containsKey(s.toString())) {
|
||
Integer value = hashMap.get(s.toString());
|
||
value = value+1;
|
||
hashMap.put(s,value);
|
||
}
|
||
}
|
||
String str1 = String.valueOf(hashMap.get("o"));
|
||
String str2 = String.valueOf(hashMap.get("x"));
|
||
// System.out.println(str1);
|
||
// System.out.println(str2);
|
||
if (hashMap.get("o")== null && hashMap.get("x")==null) {
|
||
return true;
|
||
}
|
||
else if(hashMap.get("o")!= null && hashMap.get("x")!=null && hashMap.get("o").equals(hashMap.get("x"))) {
|
||
return true;
|
||
}
|
||
else {
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="kata9-count-the-smiley-faces用正则表达式"><a class="header" href="#kata9-count-the-smiley-faces用正则表达式">Kata9 (Count the smiley faces(用正则表达式))</a></h2>
|
||
<pre><code class="language-java">/**
|
||
*Count the smiley faces!
|
||
* Rules for a smiling face:
|
||
* Each smiley face must contain a valid pair of eyes. Eyes can be marked as : or ;
|
||
* A smiley face can have a nose but it does not have to. Valid characters for a nose are - or ~
|
||
* Every smiling face must have a smiling mouth that should be marked with either ) or D
|
||
* Example
|
||
* countSmileys([':)', ';(', ';}', ':-D']); // should return 2;
|
||
* countSmileys([';D', ':-(', ':-)', ';~)']); // should return 3;
|
||
* countSmileys([';]', ':[', ';*', ':$', ';-D']); // should return 1;
|
||
* Valid smiley face examples: :) :D ;-D :~)
|
||
* Invalid smiley faces: ;( :> :} :]
|
||
*/
|
||
import java.util.*;
|
||
import java.util.stream.Collectors;
|
||
import java.util.function.*;
|
||
import java.util.TreeSet;
|
||
import java.util.stream.Stream;
|
||
class SmileFaces {
|
||
public static int countSmileys(List<String> arr) {
|
||
return (int)arr.stream().filter( x -> x.matches("[:;][-~]?[)D]")).count();
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="kata10-merge-two-list-use-stream"><a class="header" href="#kata10-merge-two-list-use-stream">Kata10 (Merge Two list use stream)</a></h2>
|
||
<pre><code class="language-java">/***
|
||
* Merge Two list use stream
|
||
*/
|
||
import java.util.*;
|
||
import java.util.stream.Collectors;
|
||
import java.util.function.*;
|
||
import java.util.TreeSet;
|
||
import java.util.stream.Stream;
|
||
class Merge{
|
||
public static List run(List<String> List1, List<String> List2){
|
||
// convert both lists into stream
|
||
Stream<String> list1 = List1.stream();
|
||
Stream<String> list2 = List2.stream();
|
||
// merge two streams
|
||
Stream<String> merged = Stream.concat(list1, list2);
|
||
// convert the merged stream into list
|
||
List<String> numbers = merged.collect(Collectors.toList());
|
||
System.out.println("Merged List: " + numbers);
|
||
return numbers;
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="kata11-stack-application-栈的实现"><a class="header" href="#kata11-stack-application-栈的实现">Kata11 (Stack Application 栈的实现)</a></h2>
|
||
<pre><code class="language-java">/**
|
||
* 包含min函数的栈
|
||
*/
|
||
import java.util.*;
|
||
import java.util.stream.Collectors;
|
||
import java.util.function.*;
|
||
import java.util.TreeSet;
|
||
import java.util.stream.Stream;
|
||
import java.util.Stack;
|
||
class StackApplication {
|
||
private Stack<Integer> dataStack = new Stack<>(); // 数据栈
|
||
private Stack<Integer> minStack = new Stack<>(); // 维护min函数的栈
|
||
public void push(int node) {
|
||
dataStack.push(node);
|
||
if (minStack.isEmpty() || minStack.peek() > dataStack.peek()) {
|
||
minStack.push(dataStack.peek()); // 当前minStack的栈顶元素大于数据栈的栈顶元素
|
||
} else {
|
||
minStack.push(minStack.peek()); // 当前minStack的栈顶元素小于数据栈的栈顶元素
|
||
}
|
||
}
|
||
public void pop() {
|
||
if (!dataStack.isEmpty()) {
|
||
dataStack.pop();
|
||
}
|
||
if (!minStack.isEmpty()) {
|
||
minStack.pop();
|
||
}
|
||
}
|
||
public int top() {
|
||
// 取出数据栈的栈顶元素
|
||
return dataStack.peek();
|
||
}
|
||
public int min() {
|
||
// 取出维护min函数的栈的栈顶元素
|
||
return minStack.peek();
|
||
}
|
||
}
|
||
public class kata {
|
||
public static void main(String[] args) throws Exception {
|
||
StackApplication st = new StackApplication();
|
||
st.push(15);
|
||
st.push(11);
|
||
st.push(12);
|
||
st.push(110);
|
||
st.push(100);
|
||
st.push(1515);
|
||
st.pop(); //delete 1515
|
||
System.out.println(st.top()); //100
|
||
System.out.println(st.min()); //11
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="kata12-findkth-solution-查找第k元素"><a class="header" href="#kata12-findkth-solution-查找第k元素">Kata12 (findKth Solution ,查找第K元素)</a></h2>
|
||
<pre><code class="language-java">/**
|
||
*findKth Solution
|
||
*/
|
||
class findKthSolution {
|
||
public static int findKth(int[] a, int n, int K) {
|
||
Arrays.sort(a);
|
||
return(a[n-K]);
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="kata15-largest-element-of-array-数组中最大的数"><a class="header" href="#kata15-largest-element-of-array-数组中最大的数">Kata15 (Largest Element Of Array 数组中最大的数)</a></h2>
|
||
<pre><code class="language-java">public class LargestElementOfArray {
|
||
public static void main(String[] args){
|
||
// Initialize array
|
||
int[] arr = new int[] { 25, 11, 7, 75, 56 };
|
||
// Initialize max with first element of array.
|
||
int max = arr[0];
|
||
// Loop through the array
|
||
for (int i = 0; i < arr.length; i++) {
|
||
// Compare elements of array with max
|
||
if (arr[i] > max)
|
||
max = arr[i];
|
||
}
|
||
System.out.println(max);
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="kata16-找出字符串中最长的回文子串长度"><a class="header" href="#kata16-找出字符串中最长的回文子串长度">Kata16 (找出字符串中最长的回文子串长度)</a></h2>
|
||
<pre><code class="language-java">/**
|
||
* 找出字符串中最长的回文子串长度
|
||
*/
|
||
public class longestPalindromeSequence {
|
||
public static int longestPalindrome(String s) {
|
||
int len = s.length();
|
||
int [][] dp = new int[len][len];
|
||
for(int i = len - 1; i>=0; i--){
|
||
dp[i][i] = 1;
|
||
for(int j = i+1; j < len; j++){
|
||
if(s.charAt(i) == s.charAt(j))
|
||
dp[i][j] = dp[i+1][j-1] + 2;
|
||
else
|
||
dp[i][j] = Math.max(dp[i+1][j], dp[i][j-1]);
|
||
}
|
||
}
|
||
return dp[0][len-1];
|
||
}
|
||
public static void main(String[] args) {
|
||
System.out.println(longestPalindromeSequence.longestPalindrome("bbbabaaabcdabcd")); //9
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="kata17-判断是否有重复部分"><a class="header" href="#kata17-判断是否有重复部分">Kata17 (判断是否有重复部分)</a></h2>
|
||
<pre><code class="language-java">import java.util.HashSet;
|
||
public class _isDuplicate {
|
||
public static void main(String[] args) {
|
||
int[] arr = {1,9,2,3,5,4};
|
||
System.out.println(containsDuplicate(arr));
|
||
}
|
||
public static boolean containsDuplicate(int[] nums) {
|
||
HashSet<Integer> set = new HashSet<>();
|
||
for (int i = 0; i < nums.length; i++) {
|
||
if (set.contains(nums[i])) {
|
||
return true;
|
||
}
|
||
set.add(nums[i]);
|
||
}
|
||
return false;
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="kata18-数组元素出现频度统计"><a class="header" href="#kata18-数组元素出现频度统计">Kata18 (数组元素出现频度统计)</a></h2>
|
||
<pre><code class="language-java">public class arrayFrequency {
|
||
public static void main(String[] args) {
|
||
// Initialize array
|
||
int[] chars = new int[] { 1, 2, 8, 3, 2, 2, 2, 5, 1 };
|
||
// Array fr will store frequencies of element
|
||
int[] fr = new int[chars.length];
|
||
for (int i = 0; i < chars.length; i++) {
|
||
int count = 1;
|
||
for (int j = i + 1; j < chars.length; j++) {
|
||
if (chars[j] == chars[i]) {
|
||
count++;
|
||
fr[j] = -1;
|
||
}
|
||
}
|
||
if (fr[i] != -1) {
|
||
fr[i] = count;
|
||
}
|
||
}
|
||
for (int i = 0; i < fr.length; i++) {
|
||
if (fr[i] != -1)
|
||
System.out.println(" " + chars[i] + " | " + fr[i]);
|
||
}
|
||
|
||
}
|
||
}
|
||
/**
|
||
*输出
|
||
* 1 | 2
|
||
2 | 4
|
||
8 | 1
|
||
3 | 1
|
||
5 | 1
|
||
*/
|
||
</code></pre>
|
||
<h2 id="kata19-java-生成随机数"><a class="header" href="#kata19-java-生成随机数">Kata19 (java 生成随机数)</a></h2>
|
||
<pre><code class="language-java">import java.util.Random;
|
||
import java.util.function.Consumer;
|
||
/**
|
||
* 生成随机数
|
||
*/
|
||
public class random {
|
||
public static void main(String[] args) {
|
||
Consumer<Integer> print = name-> {System.out.println(name);};
|
||
Random random = new Random();
|
||
int int_random = random.nextInt(); //输出int 范围内的 random number
|
||
print.accept(int_random);
|
||
int int_random1 = random.nextInt(6); //输出范围0-6
|
||
System.out.println(int_random1);
|
||
|
||
double double_random = random.nextDouble(); //输出double 范围 0-1 之间的 double number
|
||
System.out.println(double_random);
|
||
|
||
float float_random = random.nextFloat(); //输出float 范围 0-1 之间的 float number
|
||
System.out.println(float_random);
|
||
|
||
boolean boolean_random = random.nextBoolean(); //输出boolean 范围
|
||
System.out.println(boolean_random);
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="kata20-java-reverse-array-数组反转"><a class="header" href="#kata20-java-reverse-array-数组反转">Kata20 (Java Reverse Array 数组反转)</a></h2>
|
||
<pre><code class="language-java">import java.util.Arrays;
|
||
public class ReverseArray {
|
||
public static void main(String[] args) {
|
||
int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7 };
|
||
int[] reverse_array = new int[array.length];
|
||
System.out.println("orignal arr:"+ Arrays.toString(array));
|
||
for (int i = 0; i < array.length; i++) {
|
||
// System.out.println(array[array.length - 1 - i]);
|
||
reverse_array[i] = array[array.length - 1 - i];
|
||
}
|
||
// orignal arr:[1, 2, 3, 4, 5, 6, 7]
|
||
// reversed arr:[7, 6, 5, 4, 3, 2, 1]
|
||
System.out.println("reversed arr:" + Arrays.toString(reverse_array));
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="kata-21-pasrint-string-use-java-转化为int"><a class="header" href="#kata-21-pasrint-string-use-java-转化为int">Kata 21 (PasrInt String use Java 转化为Int)</a></h2>
|
||
<pre><code class="language-java">public class StrToInt {
|
||
public static int StrToInt(String str) {
|
||
if (str.length() == 0)
|
||
return 0;
|
||
char[] chars = str.toCharArray();
|
||
// 判断是否存在符号位
|
||
int flag = 0;
|
||
if (chars[0] == '+')
|
||
flag = 1;
|
||
else if (chars[0] == '-')
|
||
flag = 2;
|
||
int start = flag > 0 ? 1 : 0;
|
||
int res = 0;// 保存结果
|
||
for (int i = start; i < chars.length; i++) {
|
||
if (Character.isDigit(chars[i])) {// 调用Character.isDigit(char)方法判断是否是数字,是返回True,否则False
|
||
int temp = chars[i] - '0';
|
||
// System.out.println(temp);
|
||
res = res * 10 + temp;
|
||
// System.out.println(res);
|
||
} else {
|
||
return 0;
|
||
}
|
||
}
|
||
return flag != 2 ? res : -res;
|
||
}
|
||
public static void main(String[] args) {
|
||
// TODO Auto-generated method stub
|
||
String s = "-1231231225";
|
||
System.out.println("使用库函数转换:" + Integer.valueOf(s));
|
||
int res = StrToInt(s);
|
||
System.out.println("使用自己写的方法转换:" + res);
|
||
// 使用库函数转换:-1231231225
|
||
// 使用自己写的方法转换:-1231231225
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="kata-22-查找返回indexbinarysearch-"><a class="header" href="#kata-22-查找返回indexbinarysearch-">Kata 22 (查找返回Index,binarySearch )</a></h2>
|
||
<pre><code class="language-java">/***
|
||
* 小到大排序的数组返回index (binarySearch)
|
||
* in:{1,5,9,15,20,23},15
|
||
* out:target index 3
|
||
* can't find target return -1
|
||
* 时间复杂度 log(n)
|
||
*/
|
||
class binarySearch{
|
||
public static int search(int[] values,int target){
|
||
int left = 0;
|
||
int right = values.length-1;
|
||
int result = -1;
|
||
while(left <=right){
|
||
Integer mid = (left + right)/2;
|
||
if(values[mid]==target){
|
||
result = mid;
|
||
break;
|
||
}
|
||
else if(values[mid] <target){
|
||
left=mid+1;
|
||
}else {
|
||
right=mid-1;
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id="kata-23--list-交换相邻的元素-返回交换的list"><a class="header" href="#kata-23--list-交换相邻的元素-返回交换的list">Kata 23 ( List 交换相邻的元素 返回交换的List)</a></h2>
|
||
<pre><code class="language-java">/**
|
||
* List 交换相邻的元素 返回交换的List
|
||
*out:[2, 1, 4, 3, 6,5]
|
||
*in:[1, 2, 3, 4, 5,6]
|
||
*/
|
||
class reverse_neighbours
|
||
{
|
||
public static List<Integer> reverse_(List<Integer> array) {
|
||
List<Integer> list3 = new ArrayList<Integer>();
|
||
for (int i = 0; i < array.size(); i++) {
|
||
list3.add(0);
|
||
}
|
||
if(array.size()%2==0){
|
||
for(int i=0;i<list3.size();i++){
|
||
if(i%2==0)list3.set(i,array.get(i+1));
|
||
if(i%2==0)list3.set(i+1,array.get(i));
|
||
}
|
||
}else {
|
||
for(int i=0;i<list3.size()-1;i++){
|
||
if(i%2==0)list3.set(i,array.get(i+1));
|
||
if(i%2==0)list3.set(i+1,array.get(i));
|
||
}
|
||
list3.set(list3.size()-1,array.get(list3.size()-1));
|
||
}
|
||
System.out.println(list3);
|
||
return list3;
|
||
}
|
||
}
|
||
//System.out.println(reverse_neighbours.reverse_(Arrays.asList(new Integer[]{1, 2, 3, 4, 5})));
|
||
</code></pre>
|
||
<h2 id="kata-24-slice-list"><a class="header" href="#kata-24-slice-list">Kata 24 (slice List)</a></h2>
|
||
<pre><code class="language-java">/**
|
||
* input:(["python", "pip", "ok","java","rust","cpp","kotlin"],3,5);
|
||
* output:[java, rust, cpp]
|
||
*/
|
||
class slice_list{
|
||
public static <T> List<String> slice(List<T> asList,Integer start, Integer end) {
|
||
List<String> list1 = new ArrayList();
|
||
for(Integer s=start; s<=end; s++) {
|
||
list1.add((String) asList.get(s));
|
||
}
|
||
return list1;
|
||
}
|
||
}
|
||
//System.out.println(slice_list.slice(Arrays.asList(new String[]{"python", "pip", "ok","java","rust","cpp","kotlin"}),3,5));
|
||
</code></pre>
|
||
<h2 id="kata-25-高精度计算bigdecimal"><a class="header" href="#kata-25-高精度计算bigdecimal">Kata 25 (高精度计算,BigDecimal)</a></h2>
|
||
<pre><code class="language-java">/**
|
||
*java浮点数运算精度损失,用BigDecimal 解决
|
||
*/
|
||
class sumBigDecimal{
|
||
public static double sum(double value1,double value2){
|
||
java.math.BigDecimal big_val1 = new java.math.BigDecimal(Double.toString(value1));
|
||
java.math.BigDecimal big_val2 = new java.math.BigDecimal(Double.toString(value2));
|
||
double result = big_val1.add(big_val2).doubleValue();
|
||
return result;
|
||
}
|
||
}
|
||
//System.out.println(sumBigDecimal.sum(1.21,3.51)); //4.72
|
||
</code></pre>
|
||
<h2 id="kata-26-求一个数的平方根"><a class="header" href="#kata-26-求一个数的平方根">Kata 26 (求一个数的平方根)</a></h2>
|
||
<pre><code class="language-java">/**
|
||
* 求一个数的平方根
|
||
*/
|
||
class sqrt{
|
||
public static double sqrt(int x){
|
||
if(x==0 ||x==1)return x;
|
||
double r = x;
|
||
while(r*r>x){
|
||
r = (r+x/r)/2;
|
||
}
|
||
return r;
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h2 id=""><a class="header" href="#"></a></h2>
|
||
|
||
</main>
|
||
|
||
<nav class="nav-wrapper" aria-label="Page navigation">
|
||
<!-- Mobile navigation buttons -->
|
||
<a rel="prev" href="../../posts/java/java.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
||
<i class="fa fa-angle-left"></i>
|
||
</a>
|
||
|
||
<a rel="next prefetch" href="../../posts/java/run_jar.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||
<i class="fa fa-angle-right"></i>
|
||
</a>
|
||
|
||
<div style="clear: both"></div>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
|
||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
||
<a rel="prev" href="../../posts/java/java.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
||
<i class="fa fa-angle-left"></i>
|
||
</a>
|
||
|
||
<a rel="next prefetch" href="../../posts/java/run_jar.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||
<i class="fa fa-angle-right"></i>
|
||
</a>
|
||
</nav>
|
||
|
||
</div>
|
||
|
||
|
||
|
||
<script>
|
||
window.playground_line_numbers = true;
|
||
</script>
|
||
|
||
<script>
|
||
window.playground_copyable = true;
|
||
</script>
|
||
|
||
<script src="../../ace.js"></script>
|
||
<script src="../../editor.js"></script>
|
||
<script src="../../mode-rust.js"></script>
|
||
<script src="../../theme-dawn.js"></script>
|
||
<script src="../../theme-tomorrow_night.js"></script>
|
||
|
||
<script src="../../elasticlunr.min.js"></script>
|
||
<script src="../../mark.min.js"></script>
|
||
<script src="../../searcher.js"></script>
|
||
|
||
<script src="../../clipboard.min.js"></script>
|
||
<script src="../../highlight.js"></script>
|
||
<script src="../../book.js"></script>
|
||
|
||
<!-- Custom JS scripts -->
|
||
<script src="../../src/js/custom.js"></script>
|
||
|
||
|
||
</div>
|
||
</body>
|
||
</html>
|