dnrops.gitlink.net/posts/java/java_grammar.html

1362 lines
74 KiB
HTML
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE HTML>
<html lang="en" class="coal" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>java grammar and codewar - Andrew&#x27;s Blog</title>
<!-- Custom HTML head -->
<meta name="description" content="Andrew Ryan&#x27;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></ol></li><li class="chapter-item "><a href="../../posts/mac/mac.html"><strong aria-hidden="true">2.</strong> MacOS</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 Swift</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/how_to_create_yolov8_based_object_detection_web_service_using_python,_julia,_node.js,_javascript,_go_and_rust.html"><strong aria-hidden="true">7.25.</strong> How to create YOLOv8-based object detection web service using Python, Julia, Node.js, JavaScript, Go and Rust</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><li class="chapter-item "><a href="../../posts/python/sanctum_model_yaml.html"><strong aria-hidden="true">9.5.</strong> sanctum model yaml</a></li><li class="chapter-item "><a href="../../posts/python/how_to_detect_objects_on_images_using_the_yolov8_neural_network.html"><strong aria-hidden="true">9.6.</strong> How to detect objects on images using the YOLOv8 neural network</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> Javascript</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/zip_plain_text_attack.html"><strong aria-hidden="true">19.26.</strong> Zip plain text attack</a></li><li class="chapter-item "><a href="../../posts/ctf/3.2_PWN_note.html"><strong aria-hidden="true">19.27.</strong> PWN_note</a></li><li class="chapter-item "><a href="../../posts/ctf/3.3_pwn HCTF2016 brop.html"><strong aria-hidden="true">19.28.</strong> pwn HCTF2016 brop</a></li><li class="chapter-item "><a href="../../posts/ctf/pwn_patch_defense_skill.html"><strong aria-hidden="true">19.29.</strong> PWN Patch defense skill</a></li><li class="chapter-item "><a href="../../posts/ctf/pwn_stack_overflow.html"><strong aria-hidden="true">19.30.</strong> PWN stack overflow</a></li><li class="chapter-item "><a href="../../posts/ctf/pwn_heap_overflow.html"><strong aria-hidden="true">19.31.</strong> PWN heap overflow</a></li><li class="chapter-item "><a href="../../posts/ctf/pwn_format_string_vulnerability.html"><strong aria-hidden="true">19.32.</strong> PWN Format String Vulnerability</a></li><li class="chapter-item "><a href="../../posts/ctf/kali_linux_tutorials.html"><strong aria-hidden="true">19.33.</strong> Kali linux tutorials</a></li><li class="chapter-item "><a href="../../posts/ctf/google_dorks_2023_lists.html"><strong aria-hidden="true">19.34.</strong> Google Dorks 2023 Lists</a></li><li class="chapter-item "><a href="../../posts/ctf/dvwa_writeup.html"><strong aria-hidden="true">19.35.</strong> DVWA WriteUp</a></li><li class="chapter-item "><a href="../../posts/ctf/bwapp_writeup.html"><strong aria-hidden="true">19.36.</strong> bWAPP WriteUp</a></li><li class="chapter-item "><a href="../../posts/ctf/sqlilabs_writeup.html"><strong aria-hidden="true">19.37.</strong> sqlilabs WriteUp</a></li><li class="chapter-item "><a href="../../posts/ctf/ctf_mindmap_list.html"><strong aria-hidden="true">19.38.</strong> ctf mindmap list</a></li></ol></li></ol>
</div>
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></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&#x27;s Blog</h1>
<div class="right-buttons">
<a href="https://gitee.com/dnrops/dnrops" 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 &amp;&amp; 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&lt;1000;i++){
c.increment();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0;i&lt;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&lt;_Stream.Person&gt; people = List.of(
new _Stream.Person(&quot;Alen&quot;, MALE),
new _Stream.Person(&quot;Andrey&quot;, MALE),
new _Stream.Person(&quot;Alex&quot;, FEMALE),
new _Stream.Person(&quot;Amili&quot;, FEMALE),
new _Stream.Person(&quot;Mali&quot;, PREFER_NOT_TO_SAY)
);
// [MALE, FEMALE]
Set&lt;Gender&gt; genders = people.stream()
.map(person -&gt; person.gender)
.collect(Collectors.toSet());
System.out.println(genders);
// [Anana, Alex, Andrey, Alen, Amili]
Set&lt;String&gt; names = people.stream()
.map(person -&gt; person.name)
.collect(Collectors.toSet());
System.out.println(names);
// filter some people in List
List&lt;Person&gt; the_people_of_DONT_WANT_TO_SAY = people.stream().filter(ITEM -&gt;ITEM.gender.equals(PREFER_NOT_TO_SAY)).collect(Collectors.toList());
System.out.println(&quot;the_people_of_DONT_WANT_TO_SAY: &quot;+the_people_of_DONT_WANT_TO_SAY);
System.out.println(&quot;the_people_of_DONT_WANT_TO_SAY COUNT : &quot;+people.stream().filter(ITEM -&gt;ITEM.gender.equals(PREFER_NOT_TO_SAY)).count()); //count 满足条件的元素
// Determine the presence or absence
// .anyMatch(ITEM-&gt;ITEM.gender.equals(PREFER_NOT_TO_SAY)); //存在一个 就返回true
// .allMatch(ITEM-&gt;ITEM.gender.equals(PREFER_NOT_TO_SAY)); //所有都满足 就返回true else 返回false
// .noneMatch(ITEM-&gt;ITEM.gender.equals(PREFER_NOT_TO_SAY)); //与allMatch相反判断条件里的元素所有的都不是返回true
Boolean presence_SOME_PEOPLE_DONT_WANT_TO_SAY = people.stream().anyMatch(ITEM-&gt;ITEM.gender.equals(PREFER_NOT_TO_SAY)); //存在一个 就返回true
System.out.println(&quot;presence_SOME_PEOPLE_DONT_WANT_TO_SAY &quot;+presence_SOME_PEOPLE_DONT_WANT_TO_SAY);
}
record Person(String name, Gender gender) {
@Override
public String toString() {
return &quot;Person{&quot; +
&quot;name='&quot; + name + '\'' +
&quot;, gender=&quot; + 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(&quot;string&quot;);
sb.append(true);
sb.append(15);
sb.append(15);
sb.append(15);
sb.append(15);
sb.append(15);
sb.insert(0, &quot;first &quot;); //0 index insert value
sb.reverse(); //反转所有字符
sb.delete(0, 5); //delete index 0..=4
sb.replace(5, 8, &quot;are&quot;); //replace index 5..=7
System.out.println(&quot;Print StingBuffer : &quot;+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(&quot;string&quot;)); //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(&quot;Random Numbers between 10 and 20:&quot;);
for (int i = 0; i &lt; 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 + &quot;, &quot;);
}
// 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(&quot;/home/alen/code/java_code/learn/src/File/test.txt&quot;);
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(&quot;/home/alen/code/java_code/learn/src/File/test.txt&quot;);
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(&quot;/home/alen/code/java_code/learn/src/File/test.txt&quot;);
Scanner sc = new Scanner(file);
while (sc.hasNextLine())
System.out.println(sc.nextLine());
}
{
//without loop
File file = new File(&quot;/home/alen/code/java_code/learn/src/File/test.txt&quot;);
Scanner sc = new Scanner(file);
// we just need to use \\Z as delimiter
sc.useDelimiter(&quot;\\Z&quot;);
System.out.println(sc.next());
}
}
public static List&lt;String&gt; readFileInList(String fileName)
{
List&lt;String&gt; 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(&quot;/home/alen/code/java_code/learn/src/File/test.txt&quot;);
Iterator&lt;String&gt; 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(&quot;ReadFile_Use_BufferedReader:&quot;);
ReadFile_Use_BufferedReader();
System.out.println(&quot;ReadFile_Use_FileReader:&quot;);
ReadFile_Use_FileReader();
System.out.println(&quot;ReadFile_Use_Scanner:&quot;);
ReadFile_Use_Scanner();
System.out.println(&quot;ReadFileIntoList:&quot;);
ReadFileIntoList();
System.out.println(&quot;readFileAsString:&quot;);
String data = readFileAsString(&quot;/home/alen/code/java_code/learn/src/File/test.js&quot;);
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(&quot;/home/alen/code/java_code/learn/src/File/wrote_file.txt&quot;));
out.write(&quot;a String&quot;);
out.close();
System.out.println(&quot;File created successfully&quot;);
}
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(&quot;python /home/alen/code/java_code/learn/src/exec/test.py&quot;);
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(&quot;ls -lha&quot;);
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(&quot;Alen&quot;,null,value-&gt;System.out.println(&quot;use Consumer: last_name not defined for :&quot;+value));
hello1(&quot;Alen&quot;,null,()-&gt;System.out.println(&quot; use Runnable: last_name not defined&quot;));
}
// 用Consumer 跟javascript 的callback 一样 使用 callback
static void hello(String fist_name, String lat_name, Consumer&lt;String&gt; 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(&quot;// normal function&quot;);
greetingCustomer(new Customer(&quot;Alen&quot;, &quot;15354843621&quot;));
System.out.println(&quot;// Customer Function&quot;);
greetingCustomer_ByConsumer.accept(new Customer(&quot;Andry&quot;, &quot;19909284728&quot;));
System.out.println(&quot;// BiCustomer Function&quot;);
greetingCustomer_ByBiConsumer.accept((new Customer(&quot;Andry&quot;, &quot;19909284728&quot;)),false);
}
// use Consumer
static Consumer&lt;Customer&gt; greetingCustomer_ByConsumer = consumer-&gt;
System.out.println(&quot;Hello &quot;+consumer.CustomerName+&quot; Thanks for registering phone number &quot;+consumer.CustomerPhoneNumber);
// use void function
static void greetingCustomer(Customer customer){
System.out.println(&quot;Hello &quot;+customer.CustomerName+&quot; Thanks for registering phone number &quot;+customer.CustomerPhoneNumber);
}
// BiConsumer
static BiConsumer&lt;Customer,Boolean&gt; greetingCustomer_ByBiConsumer = (customer,showPhoneNumber) -&gt;
System.out.println(&quot;Hello &quot;+customer.CustomerName+&quot; Thanks for registering phone number &quot;+ (showPhoneNumber ? customer.CustomerPhoneNumber:&quot;***************&quot;));
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(&quot;***function***&quot;);
int inc = increment(0);
System.out.println(inc);
System.out.println(&quot;***Function***&quot;);
int inc1 = incrementFunction.apply(0);
System.out.println(inc1);
//合并两个Function
Function&lt;Integer,Integer&gt; 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(&quot;***BiFunction***&quot;);
BiFunction&lt;Integer,Integer,Integer&gt; multipleBiFunction = (num,numToMultipleBy) //2个输入参数 1个输出参数
-&gt;(num +1)*numToMultipleBy;
System.out.println(multipleBiFunction.apply(4,100));
}
// function
static int increment(int num){
num++;
return num;
}
// Function
static Function&lt;Integer,Integer&gt; incrementFunction = num -&gt; num+1;
static Function&lt;Integer,Integer&gt; multipleBy10 = num -&gt; 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(&quot;15354843621&quot;));
System.out.println(isPhoneNumberValid(&quot;153548436201&quot;));
System.out.println(isPhoneNumberValid_ByUse_Predicate.test(&quot;15354843621&quot;));
// 合并组合使用Predicate and 同时满足为true
System.out.println(&quot;// Predicate&quot;);
Boolean result = isPhoneNumberValid_ByUse_Predicate.and(containsNumber).test(&quot;15354843621&quot;);
System.out.println(result);
// 合并组合使用Predicate 和 BiPredicate &amp;&amp; 同时满足为true
System.out.println(&quot;// BiPredicate&quot;);
Boolean result1 = (isPhoneNumberValid_ByUse_Predicate.test(&quot;15354843621&quot;) &amp;&amp;containsNumber_Use_BiPredicate.test(&quot;15354843621&quot;,'9') );
System.out.println(result1);
}
static boolean isPhoneNumberValid(String phoneNumber){
return phoneNumber.startsWith(&quot;1&quot;) &amp;&amp; phoneNumber.length() ==11;
}
static Predicate&lt;String&gt; isPhoneNumberValid_ByUse_Predicate = phoneNumber-&gt;
phoneNumber.startsWith(&quot;1&quot;) &amp;&amp; phoneNumber.length() ==11;
static Predicate&lt;String&gt; containsNumber = phoneNumber -&gt;
phoneNumber.contains(&quot;3&quot;);
static BiPredicate&lt;String,Character&gt; containsNumber_Use_BiPredicate = (phoneNumber,num) -&gt;
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(&quot;normal function&quot;);
System.out.println(getConnectionUrl());
System.out.println(&quot;use Supplier &quot;);
System.out.println(getConnectionUrl_Use_Supplier.get());
}
static String getConnectionUrl(){
return &quot;jdbc://localhost5432/user&quot;;
}
static Supplier&lt;List&lt;String&gt;&gt; getConnectionUrl_Use_Supplier = ()-&gt;
List.of(&quot;jdbc://localhost5432/user&quot;,
&quot;jdbc://localhost5432/user&quot;,
&quot;jdbc://localhost5432/user&quot;
);
}
</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(&quot;Hello&quot;) =&gt; &quot;72olle&quot;
Kata.encryptThis(&quot;good&quot;) =&gt; &quot;103doo&quot;
Kata.encryptThis(&quot;hello world&quot;) =&gt; &quot;104olle 119drlo&quot;
*/
import java.util.*;
import java.util.stream.Collectors;
import java.util.function.*;
class Kata1 {
public static String encryptThis(String text) {
UnaryOperator&lt;String&gt; encrtyper = s -&gt; {
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(&quot; &quot;))
.map(s -&gt; encrtyper.apply(s))
.collect(Collectors.joining(&quot; &quot;));
}
}
</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(&quot;&quot;)){
if(s.equals(&quot;1&quot;)){
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[]{&quot;d&quot;,&quot;a&quot;,&quot;c&quot;}))); //[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(&quot;4556364607935616&quot;); // should return &quot;############5616&quot;
* Maskify.Maskify(&quot;64607935616&quot;); // should return &quot;#######5616&quot;
* Maskify.Maskify(&quot;1&quot;); // should return &quot;1&quot;
* Maskify.Maskify(&quot;&quot;); // should return &quot;&quot;
* // &quot;What was the name of your first pet?&quot;
* Maskify.Maskify(&quot;Skippy&quot;); // should return &quot;##ippy&quot;
* Maskify.Maskify(&quot;Nananananananananananananananana Batman!&quot;); // should return &quot;####################################man!&quot;
*/
import java.util.*;
import java.util.stream.Collectors;
import java.util.function.*;
class Maskify {
public static String maskify(String str) {
List&lt;String&gt; list = new ArrayList&lt;String&gt;();
for (String s : str.split(&quot;&quot;)){
list.add(s);
}
if(list.size()&gt;4){
for (int i = 0; i &lt; list.size()-4; i++) {
list.set(i,&quot;*&quot;);
}
}
// System.out.println(list.stream().collect(Collectors.joining(&quot; &quot;)));
return list.stream().collect(Collectors.joining(&quot;&quot;));
}
}
</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&lt;String, Integer&gt; hashMap = new HashMap&lt;&gt;();
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&lt;Integer&gt; set = new TreeSet&lt;&gt;();
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 = [&quot;arp&quot;, &quot;live&quot;, &quot;strong&quot;]
*
* a2 = [&quot;lively&quot;, &quot;alive&quot;, &quot;harp&quot;, &quot;sharp&quot;, &quot;armstrong&quot;]
*
* returns [&quot;arp&quot;, &quot;live&quot;, &quot;strong&quot;]
*/
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 -&gt; Arrays.stream(array2).anyMatch(s -&gt; 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的出现次数相同=&gt;true)
* Examples input/output:
*
* XO(&quot;ooxx&quot;) =&gt; true
* XO(&quot;xooxx&quot;) =&gt; false
* XO(&quot;ooxXm&quot;) =&gt; true
* XO(&quot;zpzpzpp&quot;) =&gt; true // when no 'x' and 'o' is present should return true
* XO(&quot;zzoo&quot;) =&gt; false
*/
class XO {
public static boolean getXO (String str) {
str = str.toLowerCase(Locale.ROOT); //转化为小写
HashMap&lt;String, Integer&gt; hashMap = new HashMap&lt;&gt;();
for (String s : str.split(&quot;&quot;)){
if(!hashMap.containsKey(s.toString())) {
hashMap.put(s, 0);
}
}
for (String s : str.split(&quot;&quot;)){
if(hashMap.containsKey(s.toString())) {
Integer value = hashMap.get(s.toString());
value = value+1;
hashMap.put(s,value);
}
}
String str1 = String.valueOf(hashMap.get(&quot;o&quot;));
String str2 = String.valueOf(hashMap.get(&quot;x&quot;));
// System.out.println(str1);
// System.out.println(str2);
if (hashMap.get(&quot;o&quot;)== null &amp;&amp; hashMap.get(&quot;x&quot;)==null) {
return true;
}
else if(hashMap.get(&quot;o&quot;)!= null &amp;&amp; hashMap.get(&quot;x&quot;)!=null &amp;&amp; hashMap.get(&quot;o&quot;).equals(hashMap.get(&quot;x&quot;))) {
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: ;( :&gt; :} :]
*/
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&lt;String&gt; arr) {
return (int)arr.stream().filter( x -&gt; x.matches(&quot;[:;][-~]?[)D]&quot;)).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&lt;String&gt; List1, List&lt;String&gt; List2){
// convert both lists into stream
Stream&lt;String&gt; list1 = List1.stream();
Stream&lt;String&gt; list2 = List2.stream();
// merge two streams
Stream&lt;String&gt; merged = Stream.concat(list1, list2);
// convert the merged stream into list
List&lt;String&gt; numbers = merged.collect(Collectors.toList());
System.out.println(&quot;Merged List: &quot; + 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&lt;Integer&gt; dataStack = new Stack&lt;&gt;(); // 数据栈
private Stack&lt;Integer&gt; minStack = new Stack&lt;&gt;(); // 维护min函数的栈
public void push(int node) {
dataStack.push(node);
if (minStack.isEmpty() || minStack.peek() &gt; 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 &lt; arr.length; i++) {
// Compare elements of array with max
if (arr[i] &gt; 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&gt;=0; i--){
dp[i][i] = 1;
for(int j = i+1; j &lt; 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(&quot;bbbabaaabcdabcd&quot;)); //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&lt;Integer&gt; set = new HashSet&lt;&gt;();
for (int i = 0; i &lt; 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 &lt; chars.length; i++) {
int count = 1;
for (int j = i + 1; j &lt; chars.length; j++) {
if (chars[j] == chars[i]) {
count++;
fr[j] = -1;
}
}
if (fr[i] != -1) {
fr[i] = count;
}
}
for (int i = 0; i &lt; fr.length; i++) {
if (fr[i] != -1)
System.out.println(&quot; &quot; + chars[i] + &quot; | &quot; + 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&lt;Integer&gt; print = name-&gt; {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(&quot;orignal arr:&quot;+ Arrays.toString(array));
for (int i = 0; i &lt; 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(&quot;reversed arr:&quot; + 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 &gt; 0 ? 1 : 0;
int res = 0;// 保存结果
for (int i = start; i &lt; 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 = &quot;-1231231225&quot;;
System.out.println(&quot;使用库函数转换:&quot; + Integer.valueOf(s));
int res = StrToInt(s);
System.out.println(&quot;使用自己写的方法转换:&quot; + res);
// 使用库函数转换:-1231231225
// 使用自己写的方法转换:-1231231225
}
}
</code></pre>
<h2 id="kata-22-查找返回indexbinarysearch-"><a class="header" href="#kata-22-查找返回indexbinarysearch-">Kata 22 (查找返回IndexbinarySearch )</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 &lt;=right){
Integer mid = (left + right)/2;
if(values[mid]==target){
result = mid;
break;
}
else if(values[mid] &lt;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&lt;Integer&gt; reverse_(List&lt;Integer&gt; array) {
List&lt;Integer&gt; list3 = new ArrayList&lt;Integer&gt;();
for (int i = 0; i &lt; array.size(); i++) {
list3.add(0);
}
if(array.size()%2==0){
for(int i=0;i&lt;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&lt;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:([&quot;python&quot;, &quot;pip&quot;, &quot;ok&quot;,&quot;java&quot;,&quot;rust&quot;,&quot;cpp&quot;,&quot;kotlin&quot;],3,5);
* output:[java, rust, cpp]
*/
class slice_list{
public static &lt;T&gt; List&lt;String&gt; slice(List&lt;T&gt; asList,Integer start, Integer end) {
List&lt;String&gt; list1 = new ArrayList();
for(Integer s=start; s&lt;=end; s++) {
list1.add((String) asList.get(s));
}
return list1;
}
}
//System.out.println(slice_list.slice(Arrays.asList(new String[]{&quot;python&quot;, &quot;pip&quot;, &quot;ok&quot;,&quot;java&quot;,&quot;rust&quot;,&quot;cpp&quot;,&quot;kotlin&quot;}),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&gt;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>