我正在准备一个代码,它将字符串拆分为字符数组,然后根据某种模式对每个数组进行混洗。
我已经使用了一些代码根据“keysize”将数组拆分到不同的char数组,并且它工作正常,除了可能缺少某些元素的最后一个数组。
如果最后一个数组比其他数组短,则会抛出异常,因为在某些时候交换功能可能试图将[0]与不存在的[x]交换。
那么如何将最后一个数组填充为相同的大小?
这是我在这里的一个问题中找到的分裂代码,我在其中编辑了使用 char 而不是 int 。
public static char[][] Split(char[] source, int keysize)
{
int fullArrayCount = source.Length / keysize;
int totalArrayCount = fullArrayCount;
int remainder = source.Length - (fullArrayCount * keysize);
if (remainder > 0)
{
totalArrayCount++;
}
char[][] output = new char[totalArrayCount][];
for (int i = 0; i < fullArrayCount; i++)
{
output[i] = new char[keysize];
Array.Copy(source, i * keysize, output[i], 0, keysize); // Array.Copy(Original Array,,Target array,"Where to start"0,Howmuch)
}
if (totalArrayCount != fullArrayCount)
{
output[fullArrayCount] = new char[remainder];
// output[fullArrayCount] = new char[keysize];
MessageBox.Show((keysize-remainder).ToString());
Array.Copy(source, fullArrayCount * keysize,output[fullArrayCount], 0, remainder);
}
return output;
}
答案 0 :(得分:1)
不能只是代替:
$(".widget_body").on("mousedown", "canvas", function() {
var id = $(this).attr("id");
var canvas = document.getElementById(id);
var canvas,
context,
dragging = false,
dragStartLocation,
snapshot;
fitToContainer(canvas);
function fitToContainer(canvas){
// Make it visually fill the positioned parent
canvas.style.width ='100%';
canvas.style.height='100%';
// ...then set the internal size to match
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;
}
function getCanvasCoordinates(event) {
var x = event.clientX - canvas.getBoundingClientRect().left,
y = event.clientY - canvas.getBoundingClientRect().top;
return {x: x, y: y};
}
function takeSnapshot() {
snapshot = context.getImageData(0, 0, canvas.width, canvas.height);
}
function restoreSnapshot() {
context.putImageData(snapshot, 0, 0);
}
function drawLine(position) {
context.beginPath();
context.moveTo(dragStartLocation.x, dragStartLocation.y);
context.lineTo(position.x, position.y);
context.stroke();
}
function dragStart(event) {
dragging = true;
dragStartLocation = getCanvasCoordinates(event);
takeSnapshot();
}
function drag(event) {
var position;
if (dragging === true) {
restoreSnapshot();
position = getCanvasCoordinates(event);
drawLine(position);
}
}
function dragStop(event) {
dragging = false;
restoreSnapshot();
var position = getCanvasCoordinates(event);
drawLine(position);
}
function clearCanvas(event) {
context.clearRect(0, 0, canvas.width, canvas.height);
}
context = canvas.getContext('2d');
context.strokeStyle = 'purple';
context.lineWidth = 4;
context.lineCap = 'round';
canvas.addEventListener('mousedown', dragStart, false);
canvas.addEventListener('mousemove', drag, false);
canvas.addEventListener('mouseup', dragStop, false);
canvas.addEventListener('dblclick', clearCanvas, false);
});
写:
output[fullArrayCount] = new char[remainder];
答案 1 :(得分:0)
将一组字符拆分为指定长度的字符数组的集合,用管道符号填充,可以轻松替换/删除/搜索。 (它不喜欢空炭)
public static List<char[]> Split(char[] source, int keysize)
{
List<char[]> list = new List<char[]>();
for (int i = 0; i < source.Length; i+= keysize)
{
List<char> c = source.Skip(i).Take(keysize).ToList();
while (c.Count < keysize)
{
c.Add('|');
}
list.Add(c.ToArray());
}
return list;
}
static void Main(string[] args)
{
var x = Split("abcdefgh".ToCharArray(), 3);
Console.ReadLine();
}