需要帮助在java中创建缩放列表方法吗
我正在尝试创建一个名为scaleByK的方法,它应该用k个自身的副本替换k值的每个整数。例如,如果在调用方法之前列表是:[2,4,-2,5,3,0,7],那么在方法执行之后,列表应该是[2,2,4,4,4,4,4,5,5,5,5,5,3,3,7,7,7,7]。请注意,该方法应从列表中删除所有0和负值 这就是我目前所拥有的需要帮助在java中创建缩放列表方法吗,java,Java,我正在尝试创建一个名为scaleByK的方法,它应该用k个自身的副本替换k值的每个整数。例如,如果在调用方法之前列表是:[2,4,-2,5,3,0,7],那么在方法执行之后,列表应该是[2,2,4,4,4,4,4,5,5,5,5,5,3,3,7,7,7,7]。请注意,该方法应从列表中删除所有0和负值 这就是我目前所拥有的 public void scaleByK(){ for(int i=0;i<length;i++){ if(list[i]<0||lis
public void scaleByK(){
for(int i=0;i<length;i++){
if(list[i]<0||list[i]==0){
for(int j=i+1;j<length;j++){
list[j-1]=list[j];
}
length-=1;
}
else{
for(int k=i;k<list[k]-1+i;k++){
for(int x=length-1; x>k;x--){
list[x+list[k]-1]=list[x];
}
for(int g=k+1; g<list[k+i];g++){
list[g]=list[k];
}
}
i=list[i]-1+i;
length+=list[i]-1;
}
}
}
public void scaleByK(){
对于(int i=0;i您正在覆盖此数组中的值,因此它不起作用。您可能需要创建第二个整数和大小的数组,然后填充它
尝试在迭代的每个步骤上应用print()
方法,使算法可视化并解决问题。我这里为您提供了两个版本,首先使用数组,然后仅使用数组:
public void scaleBy(int[] ints) {
ArrayList<Integer> newInts = new ArrayList<Integer>();
String myList = "";
for (Integer integer : ints) {
if (integer > 0) {
for (int i = integer; i > 0; i--) {
newInts.add(integer);
myList += integer+", ";
}
}
}
System.out.println("[ "+myList.substring(0, myList.length() - 2)+" ]");
}
public void scaleByArray(int[] ints) {
int[][] arrays = new int[10][];
int length = 0;
int arrayCount = 0;
String myList = "";
for (int integer : ints) {
if (integer > 0) {
length += integer;
int[] temp = new int[integer];
Arrays.fill(temp, integer);
arrays[arrayCount] = temp;
arrayCount++;
}
}
int[] master = new int[length];
int position = 0;
for (int[] array : arrays) {
if (array == null)
break;
for (int i = 0; i < array.length; i++) {
master[position] = array[i];
position++;
myList += array[i]+", ";
}
}
System.out.println("[ "+myList.substring(0, myList.length() - 2)+" ]");
}
public void scaleBy(int[]int){
ArrayList newInts=新的ArrayList();
字符串myList=“”;
用于(整数:整数){
如果(整数>0){
对于(int i=integer;i>0;i--){
newInts.add(整数);
myList+=整数+“,”;
}
}
}
System.out.println(“[”+myList.substring(0,myList.length()-2)+“]);
}
公共void scaleByArray(int[]int){
int[][]数组=新的int[10][];
整数长度=0;
int arrayCount=0;
字符串myList=“”;
for(整数整数:整数){
如果(整数>0){
长度+=整数;
int[]temp=新的int[整数];
数组。填充(临时、整数);
阵列[arrayCount]=温度;
arrayCount++;
}
}
int[]master=新的int[长度];
int位置=0;
for(int[]数组:数组){
if(数组==null)
打破
for(int i=0;i
两个版本输出相同的内容,但显然第一种方法更可取,如果您需要什么,请在下面进行评论
同样值得一提的是,列表
结构不能容纳像int
或double
或bool
这样的基本类型,相反,它们必须由Integer
之类的类包装,幸运的是,正如您所看到的,这对我们在这里所做的工作没有太大的影响。仅使用打印:
public static void scaleByK(int in[]){
for(int a = 0; a < in.length; a++){
if(in[a] > 0){
for(int b = 0; b < in[a]; b++){
System.out.println(in[a]);
}
}
}
}
publicstaticvoidscalebyk(int-in[]){
对于(int a=0;a0中){
for(int b=0;b
更换阵列:
public static int[] scaleByK(int in[]){
int length = 0;
for(int a = 0; a < in.length; a++){
if(in[a] > 0){
length = length + in[a];
}
}
int temp[] = new int[length];
int count = 0;
for(int b = 0; b < in.length; b++){
if(in[b] > 0){
for(int c = 0; c < in[b]; c++){
temp[count] = in[b];
count++;
}
}
}
in = new int[temp.length];
for(int d = 0; d < temp.length; d++){
in[d] = temp[d];
}
return in;
}
公共静态int[]scaleByK(int in[]中的int){
整数长度=0;
对于(int a=0;a0中){
长度=长度+英寸[a];
}
}
int temp[]=新的int[长度];
整数计数=0;
对于(int b=0;b0中){
for(int c=0;c
首先,使用可以调整大小的结构(最简单的方法是实现java.util.List
)。
public static int[] scaleByK(int in[]){
int length = 0;
for(int a = 0; a < in.length; a++){
if(in[a] > 0){
length = length + in[a];
}
}
int temp[] = new int[length];
int count = 0;
for(int b = 0; b < in.length; b++){
if(in[b] > 0){
for(int c = 0; c < in[b]; c++){
temp[count] = in[b];
count++;
}
}
}
in = new int[temp.length];
for(int d = 0; d < temp.length; d++){
in[d] = temp[d];
}
return in;
}