簡體   English   中英

如何在沒有定義長度的情況下定義數組

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

問題是:獲得10分的分數; 那么您需要問得分高於80的每個人是否想繼續學習“ y”(是)和“ n”(否)。 接下來,您需要獲取他們在數組中的位置,因此,如果人數5的數組array [5] = 90並回答“ y”,則將使用newarray [1] = [5(他的位置)]創建一個新數組

我的問題是如何在不知道新數組長度的情況下定義新數組(會有多少個“ y”)。 編輯:

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
            }
        }
    }


}

}

您可以創建一個與整個列表大小相同的臨時數組(因為您知道這可能是最大的數組),並在其中填充盡可能多的學生以選擇繼續。 實際上,臨時數組中的元素可能會少於它可以容納的元素,因為有些學生不會繼續學習,因此您可以創建一個大小合適的新數組(現在就知道了),然后使用System.arraycopy()進行操作。將所有元素復制到正確大小的數組中。

這不是執行此操作的最有效方法,但它並不可怕(它僅使用一個額外的數組分配和復制操作),並且對於家庭作業當然足夠好。 而且它不使用數組以外的任何東西。

通常,這是您在編寫程序時會不時使用的一種技術:如果有些事情直到您完成一些操作后才能做,然后尋找一種將操作分為多個步驟的方法,以便您可以以可能的順序重新排列步驟,但仍會產生相同的結果。 更好的辦法是找到一個滿足您需求的不同數據結構(就像ArrayList一樣,因為它的容量可以在運行時增加),但是有時候您不能只是放棄一個不同的數據結構,這種方法分解問題並重新安排步驟可能會很有用。

在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

取決於您的編程語言。在Java中,必須先聲明數組中的空間量,然后才能使用它。 這是因為Java為數組留出了空間。 其他語言(例如Python)不需要您聲明使用的空間量。 Java中有多種方法可以解決此問題,例如使用arraylist。

使用數組列表。 如果他拒絕,則將其刪除。 最后你

yourArrayList.toArray()

將arrayList轉換為數組。

編輯:

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();


}

數組列表

創建數組列表是為了解決數組的長度問題。

在您的示例中:

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
        }
    }
}

您想做這樣的事情:

 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);
        }
    }
}

然后,您可以使用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