正在尝试在Java中的数组中创建AddBefore方法。不使用ArrayList 公共类数组{ 私有静态int[]vals={1,3,2,5}; public void addBefore(int-input,int-before){ int[]temp=新int[vals.length*2]; //int[]temp2=新int[vals.length*2]; int位置=0; 布尔检查=假; 对于(int i=0;i
您是否正在寻找类似的内容:正在尝试在Java中的数组中创建AddBefore方法。不使用ArrayList 公共类数组{ 私有静态int[]vals={1,3,2,5}; public void addBefore(int-input,int-before){ int[]temp=新int[vals.length*2]; //int[]temp2=新int[vals.length*2]; int位置=0; 布尔检查=假; 对于(int i=0;i,java,arrays,Java,Arrays,您是否正在寻找类似的内容: public class Arrays { private static int[] vals = {1,3,2,5}; public void addBefore(int input, int before){ int[] temp = new int[vals.length*2]; //int[] temp2 = new int [vals.length * 2]; int position =
public class Arrays {
private static int[] vals = {1,3,2,5};
public void addBefore(int input, int before){
int[] temp = new int[vals.length*2];
//int[] temp2 = new int [vals.length * 2];
int position = 0;
boolean check = false;
for(int i = 0; i<vals.length; i++){
check = false;
if(vals[i] == (before)){
check = true;
temp[i] = input;
position = i;
break;
}
temp[i] = vals[i];
}
vals[position] = input;
int previous = input;
int current = 0;
for(int i=position;i<vals.length;i++){
current = vals[i];
vals[i] = previous;
previous = current;
}
}
public static void main(String[] args){
Arrays a = new Arrays();
a.addBefore(1, 2);
for(int i = 0; i<vals.length; i++){
System.out.println(vals[i]);
}
}
}
公共静态int[]addBefore(int[]array,int-input,int-before)
{
//创建一个新阵列,再增加1个空间
int[]newArray=newint[array.length+1];
//在原始阵列中复制到插入点
for(int x=0;x 对于(int i=0;i您是否正在寻找类似的内容:
public class Arrays {
private static int[] vals = {1,3,2,5};
public void addBefore(int input, int before){
int[] temp = new int[vals.length*2];
//int[] temp2 = new int [vals.length * 2];
int position = 0;
boolean check = false;
for(int i = 0; i<vals.length; i++){
check = false;
if(vals[i] == (before)){
check = true;
temp[i] = input;
position = i;
break;
}
temp[i] = vals[i];
}
vals[position] = input;
int previous = input;
int current = 0;
for(int i=position;i<vals.length;i++){
current = vals[i];
vals[i] = previous;
previous = current;
}
}
public static void main(String[] args){
Arrays a = new Arrays();
a.addBefore(1, 2);
for(int i = 0; i<vals.length; i++){
System.out.println(vals[i]);
}
}
}
公共静态int[]addBefore(int[]array,int-input,int-before)
{
//创建一个新阵列,再增加1个空间
int[]newArray=newint[array.length+1];
//在原始阵列中复制到插入点
for(int x=0;x 对于(int i=0;iremovevals[position]=input;
public static int[] addBefore(int[] array, int input, int before)
{
//create a new array with 1 more space
int[] newArray = new int[array.length + 1];
//copy in the original array up to the insertion point
for (int x = 0; x < before; x++)
{
newArray[x] = array[x];
}
//element at index 'before' will be pushed one forward; 'before' is now the new one
newArray[before] = input;
//fill in the rest
for (int x = before + 1; x < newArray.length; x++)
{
//need to offset it by one to account for the added int
newArray[x] = array[x - 1];
}
return newArray;
}
public static void main(String[] args)
{
int[] vals = {1,3,2,5};
for(int i = 0; i<vals.length; i++){
System.out.print(vals[i] + ",");
}
System.out.println();
vals = addBefore(vals, 1, 2);
for(int i = 0; i<vals.length; i++){
System.out.print(vals[i] + ",");
}
}
公共类数组{
私有静态int[]vals={1,3,2,5};
public void addBefore(int-input,int-before){
int[]temp=新int[vals.length*2];
//int[]temp2=新int[vals.length*2];
int位置=0;
布尔检查=假;
对于(int i=0;i
删除VAL[位置]=输入;
public static int[] addBefore(int[] array, int input, int before)
{
//create a new array with 1 more space
int[] newArray = new int[array.length + 1];
//copy in the original array up to the insertion point
for (int x = 0; x < before; x++)
{
newArray[x] = array[x];
}
//element at index 'before' will be pushed one forward; 'before' is now the new one
newArray[before] = input;
//fill in the rest
for (int x = before + 1; x < newArray.length; x++)
{
//need to offset it by one to account for the added int
newArray[x] = array[x - 1];
}
return newArray;
}
public static void main(String[] args)
{
int[] vals = {1,3,2,5};
for(int i = 0; i<vals.length; i++){
System.out.print(vals[i] + ",");
}
System.out.println();
vals = addBefore(vals, 1, 2);
for(int i = 0; i<vals.length; i++){
System.out.print(vals[i] + ",");
}
}
公共类数组{
私有静态int[]vals={1,3,2,5};
public void addBefore(int-input,int-before){
int[]temp=新int[vals.length*2];
//int[]temp2=新int[vals.length*2];
int位置=0;
布尔检查=假;
对于(int i=0;i