简体   繁体   English

如何在没有定义长度的情况下定义数组

[英]how to define an array without a defined length of it

The question is: get a score of 10 peoples; 问题是:获得10分的分数; then you need to ask each one with a score higher than 80 if he would want to continue studying 'y' for yes and 'n' for no. 那么您需要问得分高于80的每个人是否想继续学习“ y”(是)和“ n”(否)。 Next you need to get their location in the array so if person number 5 array[5]=90 and answers 'y' it will make a new array with newarray[1]=[5(his location)] 接下来,您需要获取他们在数组中的位置,因此,如果人数5的数组array [5] = 90并回答“ y”,则将使用newarray [1] = [5(他的位置)]创建一个新数组

My question is how to define the newarray without knowing its length(how many 'y' there would be). 我的问题是如何在不知道新数组长度的情况下定义新数组(会有多少个“ y”)。 EDIT: 编辑:

import java.util.Scanner;
public class p44_52 {
static Scanner reader = new Scanner(System.in);
static void input(int []a)
{
    for(int i=0; i<a.length;i++){
        System.out.println("Write an score of a student");
        a[i]= reader.nextInt();
    }
}

static void output(int []b)
{
    for (int i =0; i<b.length;i++)
    {
        System.out.println("serial number of who choose to continue and score above 80  "+ b[i]);
    }
}

public static void main(String[]args){

    char c = 'g';
    int count =0;
    int []score = new int[10];
    kelet(score);

    for(int i=0; i<score.length;i++)
    {
        if(score[i]>80)
        {
            System.out.println("Studnet number "+i+ " Do you want to continue?");
            c = reader.next().charAt(0);
            if(c == 'y'){
                //Here i want to make the new array(length isn't known) for their locationnumbers
            }
        }
    }


}

} }

You can create a temporary array with the same size as the full list (because you know that's the largest it could need to be) and populate it with as many students as choose to continue. 您可以创建一个与整个列表大小相同的临时数组(因为您知道这可能是最大的数组),并在其中填充尽可能多的学生以选择继续。 There will probably be fewer elements actually in the temporary array than it can hold because some students won't continue, so then you can create a new array that's the right size (now that you know it) and use System.arraycopy() to copy all of the elements into the right-size array. 实际上,临时数组中的元素可能会少于它可以容纳的元素,因为有些学生不会继续学习,因此您可以创建一个大小合适的新数组(现在就知道了),然后使用System.arraycopy()进行操作。将所有元素复制到正确大小的数组中。

It's not the most efficient way to do it, but it's not terrible (it just uses one extra array allocation and copy operation) and it's certainly good enough for homework. 这不是执行此操作的最有效方法,但它并不可怕(它仅使用一个额外的数组分配和复制操作),并且对于家庭作业当然足够好。 And it doesn't use anything other than arrays. 而且它不使用数组以外的任何东西。

In general, this is a technique you'll use from time to time as you write programs: if there's something you can't do until you've done some operation, then look for a way to break the operation into multiple steps so you can rearrange steps in an order that is possible but still yields the same result. 通常,这是您在编写程序时会不时使用的一种技术:如果有些事情直到您完成一些操作后才能做,然后寻找一种将操作分为多个步骤的方法,以便您可以以可能的顺序重新排列步骤,但仍会产生相同的结果。 Better still is to find a different data structure that meets your needs (as ArrayList will, since its capacity can be grown at runtime), but there will be times when you can't just drop in a different data structure, and this approach of breaking the problem down and rearranging steps can be useful then. 更好的办法是找到一个满足您需求的不同数据结构(就像ArrayList一样,因为它的容量可以在运行时增加),但是有时候您不能只是放弃一个不同的数据结构,这种方法分解问题并重新安排步骤可能会很有用。

in java you can use ArrayList instead of Array if you don't want to give initial capacity.. 在Java中,如果您不想提供初始容量,则可以使用ArrayList而不是Array。

List<Integer> list= new ArrayList<Integer>(); 
list.add(10); //you can add elements dynmically.

//to get data you can use 
list.get(0); //index at which you want data

Depends on the language your programming in. In java have to declare the amount of space in the array before you can use it. 取决于您的编程语言。在Java中,必须先声明数组中的空间量,然后才能使用它。 This is because Java sets aside the space for the array. 这是因为Java为数组留出了空间。 Other languages such as Python dont require you to declare the amount of space used. 其他语言(例如Python)不需要您声明使用的空间量。 There are ways of getting around this in Java like using the arraylist. Java中有多种方法可以解决此问题,例如使用arraylist。

Use an arraylist. 使用数组列表。 If he says no then you remove it. 如果他拒绝,则将其删除。 At the end you 最后你

yourArrayList.toArray()

to convert the arrayList to an array. 将arrayList转换为数组。

EDIT : 编辑:

public static void main(String[]args){

  char c = 'g';
  int count =0;
  int []score = new int[10];
  ArrayList<Integer> list = new ArrayList<Integer>();
  kelet(score);

  for(int i=0; i<score.length;i++)
  {
    if(score[i]>80)
    {
        System.out.println("Studnet number "+i+ " Do you want to continue?");
        c = reader.next().charAt(0);
        if(c == 'y'){
            list.add(//your variable);
        }
    }
  }

  //If you want turn it into array
  int [] listArray = list.toArray();


}

ArrayLists 数组列表

Arraylists were created to solve the length issue of arrays. 创建数组列表是为了解决数组的长度问题。

in your example: 在您的示例中:

for(int i=0; i<score.length;i++)
{
    if(score[i]>80)
    {
        System.out.println("Studnet number "+i+ " Do you want to continue?");
        c = reader.next().charAt(0);
        if(c == 'y'){
            //Here i want to make the new array(length isn't known) for their locationnumbers
        }
    }
}

you want to do something like this: 您想做这样的事情:

 List<String> continueStudy = new ArrayList<String>();
 for(int i=0; i<score.length;i++)
 {
    if(score[i]>80)
    {
        System.out.println("Studnet number "+i+ " Do you want to continue?");
        c = reader.next().charAt(0);
        if(c == 'y'){
            continueStudy.add(c);
        }
    }
}

Then you can use a for loop to evaluate each string such as: 然后,您可以使用for循环来评估每个字符串,例如:

for(String Value : continueStudy){
    if(value.contains('y')){
       //DO SOMETHING or call seperate function
    }
}

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

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