Java 如何将数组划分为不同的';杂志';
我有这个项目,我一直在工作,但我有点生疏,所以我可以使用一些帮助来找出这一部分 我的项目的主要目的是,我希望能够向目标开火,让目标分析我的数据 在我开始做这件事之前,我做了一些研究,想知道以前是否有人做过这件事,结果发现美国的一家公司做过,但方式与我想象的不同 这不是一个商业想法,只是我自己的一个有趣的项目:) 现在是我的问题,我当然做了一个没有枪和目标的“原型”。 所有的文本都是Java的,看看我是否能做到这一点。 自从我生锈(现在仍然如此)以来,我做了一些研究,找出了我面临的大部分问题 让我卡住的部分在它的最后,我有一个用于所有镜头的阵列,但我想根据弹匣容量的大小将这个阵列分成多个阵列 我将如何做到这一点 不幸的是,如果我选择删除代码,我就不会保存代码,所以我的代码碎片早就不存在了,但我已经尝试了一些我想到的事情,好几天我都没能弄明白这一点 我尝试了嵌套for循环,然后又尝试了一次,但在while循环中使用了嵌套for循环,我可能也尝试了一些不同的方法Java 如何将数组划分为不同的';杂志';,java,Java,我有这个项目,我一直在工作,但我有点生疏,所以我可以使用一些帮助来找出这一部分 我的项目的主要目的是,我希望能够向目标开火,让目标分析我的数据 在我开始做这件事之前,我做了一些研究,想知道以前是否有人做过这件事,结果发现美国的一家公司做过,但方式与我想象的不同 这不是一个商业想法,只是我自己的一个有趣的项目:) 现在是我的问题,我当然做了一个没有枪和目标的“原型”。 所有的文本都是Java的,看看我是否能做到这一点。 自从我生锈(现在仍然如此)以来,我做了一些研究,找出了我面临的大部分问题 让我
if (hasrun == true) {
//OUTPUT
ms(2);
System.out.print("You shot the following:");
nl(1);
int counter = 0;
int i = 0;
while (sf > magcap) {
sf-=magcap;
mnm++;
}
while (counter <= mnm) {
System.out.print("Mag " + mn + ": ");
for (i+=0; i < magcap; i++) {
System.out.print(shots[i] + " ");
}
counter++;
mn++;
nl(1);
}
if(hasrun==true){
//输出
ms(2);
System.out.print(“您拍摄了以下内容:”);
nl(1);
int计数器=0;
int i=0;
while(sf>magcap){
sf-=磁电容;
mnm++;
}
而(counter你可以用番石榴轻松地做
int[] x = {1,2,3,4,5,6};
int splitSize = 3;
List<Integer> list = IntStream.of(x).boxed().collect(Collectors.toList());
//partition
List<List<Integer>> partitioned = Lists.partition(list, splitSize);
//result: [[1,2,3], [4,5,6]]
int[]x={1,2,3,4,5,6};
int splitSize=3;
List List=IntStream.of(x).boxed().collect(Collectors.toList());
//分割
List partitioned=Lists.partition(List,splitSize);
//结果:[[1,2,3],[4,5,6]]
您可以使用番石榴轻松制作
int[] x = {1,2,3,4,5,6};
int splitSize = 3;
List<Integer> list = IntStream.of(x).boxed().collect(Collectors.toList());
//partition
List<List<Integer>> partitioned = Lists.partition(list, splitSize);
//result: [[1,2,3], [4,5,6]]
int[]x={1,2,3,4,5,6};
int splitSize=3;
List List=IntStream.of(x).boxed().collect(Collectors.toList());
//分割
List partitioned=Lists.partition(List,splitSize);
//结果:[[1,2,3],[4,5,6]]
我有点明白了,下面是我所做的。还没有完全实现,但这在数组中起作用
// OUTPUT
while (sf >= magcap) {
sf -= magcap;
mn++;
}
ms(2);
System.out.println(mn + " mags and " + sf + " shots fired");
nl(2);
for (int i = 0; i < mn; i++) {
from = magcap * magcounter - magcap;
to = magcap * magcounter;
System.out.print("Mag " + magcounter + ": ");
for (int j = from; j < to - 1; j++) {
System.out.print(shots[j] + ", ");
}
System.out.print(shots[to - 1] + ".");
magcounter++;
nl(1);
}
// PRINT THE REST
if (sf >= 1) {
from = magcap * magcounter - magcap;
to = magcap * magcounter;
if (sf == 1) {
System.out.print("Mag " + magcounter + ": ");
System.out.print(shots[from] + ".");
} else {
System.out.print("Mag " + magcounter + ": ");
for (int i = 0; i < sf - 1; i++) {
System.out.print(shots[from + i] + ", ");
}
from += sf -1;
System.out.print(shots[from] + ".");
}
}
我有点搞明白了,这就是我所做的。还没有完全实现,但这是一个数组
// OUTPUT
while (sf >= magcap) {
sf -= magcap;
mn++;
}
ms(2);
System.out.println(mn + " mags and " + sf + " shots fired");
nl(2);
for (int i = 0; i < mn; i++) {
from = magcap * magcounter - magcap;
to = magcap * magcounter;
System.out.print("Mag " + magcounter + ": ");
for (int j = from; j < to - 1; j++) {
System.out.print(shots[j] + ", ");
}
System.out.print(shots[to - 1] + ".");
magcounter++;
nl(1);
}
// PRINT THE REST
if (sf >= 1) {
from = magcap * magcounter - magcap;
to = magcap * magcounter;
if (sf == 1) {
System.out.print("Mag " + magcounter + ": ");
System.out.print(shots[from] + ".");
} else {
System.out.print("Mag " + magcounter + ": ");
for (int i = 0; i < sf - 1; i++) {
System.out.print(shots[from + i] + ", ");
}
from += sf -1;
System.out.print(shots[from] + ".");
}
}
使用System.arraycopy
:
import java.util.Arrays;
public class Testing {
// static int[] input = {1,2,3,4,5,6}; // testdata
static int[] input = {1,2,3,4,5,6,7}; // testdata
// static int[] input = {1,2,3,4,5,6,7,8}; // testdata
static int magSize = 3; // testdata
// static int magSize = 5; // testdata
public static void main(String[] args) {
// int resultSize = (int) Math.ceil((double) input.length / magSize);
int resultSize = input.length % magSize == 0 ?
input.length/magSize :
input.length/magSize + 1;
int[][] result = new int[resultSize][magSize];
for (int i = 0; i < resultSize; i++) {
System.arraycopy(input, // src
i*magSize, // srcPos
result[i], // dest
0, // destPos
(i + 1) * magSize < input.length ?
magSize :
input.length - i*magSize); // length
}
System.out.println("Original : " + Arrays.toString(input));
System.out.println("Result : " + Arrays.deepToString(result));
/*
prints:
Original : [1, 2, 3, 4, 5, 6, 7]
Result : [[1, 2, 3], [4, 5, 6], [7, 0, 0]]
*/
}
}
导入java.util.array;
公共类测试{
//静态int[]输入={1,2,3,4,5,6};//testdata
静态int[]输入={1,2,3,4,5,6,7};//testdata
//静态int[]输入={1,2,3,4,5,6,7,8};//testdata
static int magSize=3;//testdata
//static int magSize=5;//testdata
公共静态void main(字符串[]args){
//int resultSize=(int)Math.ceil((double)input.length/magSize);
int resultSize=input.length%magSize==0?
input.length/magSize:
input.length/magSize+1;
int[]result=newint[resultSize][magSize];
for(int i=0;i
使用系统。阵列复制
:
import java.util.Arrays;
public class Testing {
// static int[] input = {1,2,3,4,5,6}; // testdata
static int[] input = {1,2,3,4,5,6,7}; // testdata
// static int[] input = {1,2,3,4,5,6,7,8}; // testdata
static int magSize = 3; // testdata
// static int magSize = 5; // testdata
public static void main(String[] args) {
// int resultSize = (int) Math.ceil((double) input.length / magSize);
int resultSize = input.length % magSize == 0 ?
input.length/magSize :
input.length/magSize + 1;
int[][] result = new int[resultSize][magSize];
for (int i = 0; i < resultSize; i++) {
System.arraycopy(input, // src
i*magSize, // srcPos
result[i], // dest
0, // destPos
(i + 1) * magSize < input.length ?
magSize :
input.length - i*magSize); // length
}
System.out.println("Original : " + Arrays.toString(input));
System.out.println("Result : " + Arrays.deepToString(result));
/*
prints:
Original : [1, 2, 3, 4, 5, 6, 7]
Result : [[1, 2, 3], [4, 5, 6], [7, 0, 0]]
*/
}
}
导入java.util.array;
公共类测试{
//静态int[]输入={1,2,3,4,5,6};//testdata
静态int[]输入={1,2,3,4,5,6,7};//testdata
//静态int[]输入={1,2,3,4,5,6,7,8};//testdata
static int magSize=3;//testdata
//static int magSize=5;//testdata
公共静态void main(字符串[]args){
//int resultSize=(int)Math.ceil((double)input.length/magSize);
int resultSize=input.length%magSize==0?
input.length/magSize:
input.length/magSize+1;
int[]result=newint[resultSize][magSize];
for(int i=0;i
这回答了你的问题吗?我刚刚发现这篇文章,看起来是一样的。我现在就看。这回答了你的问题吗?我刚刚发现这篇文章,看起来是一样的。我现在就看。我不知道什么是番石榴,你能详细说明一下你的答案吗?这是来自谷歌。请参阅此处的安装说明:.Doc:或烹饪书:我不知道番石榴是什么,您能详细说明一下您的答案吗?这是谷歌提供的java库。请参阅此处的安装说明:.Doc:或