更改数组元素?

时间:2017-03-30 20:20:20

标签: java arrays methods element computer-science

我的目标是有一个像这样的数组int arrMarks [] = {10,-15,25,102,30},当元素低于零或高于100时,我需要使它等于零。然后这些元素应该在数组的最后一个位置,另一个位置移除一个位置。那么如何在不使用任何包装或短缺的情况下实现这一目标呢?

public void validateMarks(int [] array){
        int [] array1 = new int[5];

        for(int i = 0; i<array.length; i++){
            if(array[i] < 0 || array[i] > 100){
                int f= array1.length-1;

                array1[f] = array[i];
                array1[f] = 0;
                f--;
            }

            else if(array[i] > 0 || array[i] < 100){

                array1[i] = array[i];

            }
            System.out.println(array1[i]);
        }
    }

3 个答案:

答案 0 :(得分:0)

你可以使用方法swap。

public void swap(int i, int j, int[] arr) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;

答案 1 :(得分:0)

DateTime finaldate = DateTime.ParseExact(mydate, "ddd MMM dd, yyyy h:mm tt", CultureInfo.InvariantCulture);

这里要学到的经验教训:

  • 不要硬编码值( public static void SelectFile(string sourceId) { string serverPath; string filename; byte[] serverTxn; using (TransactionScope ts = new TransactionScope()) { using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["DBConn"].ToString())) { conn.Open(); using (SqlCommand cmd = new SqlCommand("OPS.LoadFileBlobFromSQL", conn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@AttachmentId", SqlDbType.VarChar).Value = sourceId; using (SqlDataReader rdr = cmd.ExecuteReader()) { rdr.Read(); filename = rdr.GetSqlString(0).Value; serverPath = rdr.GetSqlString(1).Value; serverTxn = rdr.GetSqlBinary(2).Value; rdr.Close(); } } StreamObjectFromFilestream(serverPath, serverTxn, filename); } ts.Complete(); } } private static void StreamObjectFromFilestream(string serverPath, byte[] serverTxn, string filename) { SqlFileStream sfs = new SqlFileStream(serverPath, serverTxn, FileAccess.Read); byte[] buffer = new byte[sfs.Length]; sfs.Read(buffer, 0, buffer.Length); System.IO.File.WriteAllBytes(@"c:\test\hello.pdf", buffer); sfs.Close(); } 更改为public void validateMarks(int[] array){ int[] array1 = new int[array.length]; int pos = 0; for (int i = 0; i < array.length; i++){ if (array[i] >= 0 && array[i] <= 100) array1[pos++] = array[i]; } for (int i = 0; i < array.length; i++){ System.out.println(array1[i]); } }
  • 了解范围规则(5array.length需要在f循环之外声明)
  • 有时您不需要存储值

最后一点暗示pos实际上并不是必需的。我把这个挑战留给了OP。

答案 2 :(得分:0)

好吧,让我检查一下我是否正确理解它。根据我的理解,你想要检查每个数组值是否小于0或大于100.如果是这样,应该从原始数组中删除该值(替换为0)并复制到新数组的最后位置。所以,如果我得到它,有我的建议:

public int[] validateMarks(int [] originalArray){
    int[] returnArray = new Int[originalArray.length];
    int i,j;

    j=originalArray.length; //returnArray will be visited backwards

    for(i=0;i<originalArray.length;i++){ //originalArray will be visited forwards
        if(originalArray[i]<0 || originalArray[i]>100){
            returnArray[j] = originalArray[i]
            //Last valid returnArray's position will receive originalArray's value which, is less than 0 or higher than 1000
            originalArray[i] = 0;
            //The value which was less then 0 or higher than 100 will be replaed by 0 at the originalArray
            j--;
            //returnArray's counter is decreased so it can be visited backwards
        }
    }

    return returnArray;
    //The new processed array will be returned by this method. If you prefer, you can set this method as void and work it your way.
}

希望它对你有所帮助。干杯!