简体   繁体   English

用户输入一个整数,该整数将添加到数组中-Java

[英]User enters a int, which is added to an array - Java

psudeo code of what i want the add() method to do. 我想要add()方法执行的伪代码。 Pretty much the user is prompted to type in a number. 几乎提示用户输入数字。 Once they type in a number, the number will be added to the array[0] and every other value will be pushed up one. 一旦他们输入数字,该数字将被添加到array [0],而其他所有值将被上推一个。 However the array is only 20.length. 但是,数组只有20.length。 So every array[20] is deleted. 因此,删除每个数组[20]。 (ill add in the loop so they're still getting asked). (请添加循环,以便仍然被询问)。 Please help me make this add method, or show me a java method that does this. 请帮助我制作此add方法,或向我展示执行此操作的java方法。 (not using the .list method). (不使用.list方法)。 thanks in advance. 提前致谢。

public static void main(String[] args) {
    int[] array = new int[1];
    Scanner sc = new Scanner(System.in);

    int num;
    num = sc.nextInt();
    add(num); 

    for(int i = 0; i < array.length; i++) {
        println(array[i]);
    }
}

public void add(int value) {
    array[0] = value;

    for(int i = 0; i < array.length; i++) {    
      if(array[i + 1] == already has value) {
         // then shift numbers one to the right;
      } else if(array.length > 20) {
         //then delete array[19];
      }
  }
}

You are going to want to shift the entire list left before adding the value. 您将要在添加值之前将整个列表向左移动。 Consider this algorithm: 1. start at the second to last element of the list and shift it to the right overwriting what existed at the last element. 请考虑以下算法:1.从列表的倒数第二个元素开始,然后将其移至右侧,以覆盖最后一个元素中存在的内容。 2. do this for all values down to the first one. 2.对所有值(直到第一个)执行此操作。 3. set array[0] to value 3.将array [0]设置为value

You are done. 大功告成

Arrays are "contiguous blocks of memory", so in order to add a value to the start of these blocks, you'll have to first shift the rest values to the corresponding block. 数组是“连续的内存块”,因此为了在这些块的开头添加一个值,您必须首先将其余值移到相应的块。

public static int[] arr = {1,2,3,4,5,6,7,8,9,10};

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    int num;
    num = sc.nextInt();
    add(num);


    for(int i = 0; i < arr.length; i++) {
        System.out.println(arr[i]);
    }
}

public static void add(int value) {
    for(int i=arr.length-1;i>0;i--){
        arr[i] = arr[i-1];        //first shifting these values to make space for the new element at zero index
    }
    arr[0] = value;    
}   

You can do as MadProgrammer said in your comment or better use ArrayList : 您可以按照MadProgrammer在评论中所说的做,也可以更好地使用ArrayList

From add(int index, E element) add(int index,E元素)

Inserts the specified element at the specified position in this list. 将指定的元素插入此列表中的指定位置。 Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). 将当前在该位置的元素(如果有)和任何后续元素右移(将其索引添加一个)。

Example(Change it according to your need): 示例(根据需要进行更改):

List<Integer> list = new ArrayList<Integer>(20);
public void add(int value) {

  list.add(0, value);
  for(int i = 0; i < array.length; i++) {
     list.add(index, value); // this will automatically shift element right
     if(list.size > 20) {
       list.remove(20);
      }
   }
}

Any reason why you don't wish to use ArrayList? 您为什么不希望使用ArrayList的任何原因? In case no, any chance that this is not a programming assignment :) 如果没有,这可能不是编程任务:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM