flhs笔试题-回家上机实践
这是最近参加的一个公司的笔试题,回家上机写了下代码,希望对有需要的小伙伴有用,简单实现字符串和数组在指定位置的插入:
package org.flhs;
import com.google.common.base.Strings;
/**
* Created with IntelliJ IDEA.
* User: cutter.li
* Date: 14-3-7
* Time: 下午3:10
* To change this template use File | Settings | File Templates.
*/
public class FLHSTest {
public static void main(String[] args) {
// changePosition("iloveyou", 5);
Object[] objArray= insertObjArrayInPosition(new Object[]{"a","b","hello"},new Object[]{"fuck","you"},3);
printArray(objArray);
}
private static void printArray(Object[] objArray) {
if(null!=objArray&&objArray.length>0)
{
for(Object obj:objArray)
{
System.out.print(obj+" , ");
}
}
}
//对指定位置前后的字符串的内容进行调换
public static void changePosition(String str, int position) {
String res = "";
if (Strings.isNullOrEmpty(str)) {
res = "改变的字符串为空";
} else {
int len = str.length();
if (position <= 0 || position >= len) {
res = str;
} else {
String preTxt = str.substring(0, position);
String remainTxt = str.substring(position, str.length());
res = remainTxt + preTxt;
}
}
System.out.println(res);
}
//数组在指定位置的插入
public static Object[] insertObjArrayInPosition(Object[] dest, Object[] source, int position) {
if (null == dest) {
return source;
}
if (null == source) {
return dest;
}
int destSize = dest.length;
int sourceSize = source.length;
Object[] resObjArray = new Object[destSize + sourceSize];
if (position <= 0) {
System.arraycopy(source, 0, resObjArray, 0, sourceSize);
System.arraycopy(dest , 0, resObjArray, sourceSize, destSize);
} else if (position > dest.length) {
System.arraycopy(dest, 0, resObjArray, 0, destSize);
System.arraycopy(source, 0, resObjArray, destSize, sourceSize);
} else {
System.arraycopy(dest, 0, resObjArray, 0, position);
System.arraycopy(source, 0, resObjArray, position, sourceSize);
System.arraycopy(dest, position, resObjArray, position+sourceSize, destSize - position);
}
return resObjArray;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2011-03-07 使用用户自定义控件实现asp.net的的权限管理