动态数组,C语言如何动态分配二维数组

伏羲号

动态数组,C语言如何动态分配二维数组?

使用malloc函数,先分配第一维的大小,然后再循环分配每一维的大小。示例代码,分配3行4列二维数组:

动态数组,C语言如何动态分配二维数组

#include <stdio.h>#include <malloc.h>int main(){ int **a; int i, j; a = (int**)malloc(sizeof(int*)*3)

;//为二维数组分配3行 for (i = 0; i < 3; ++i){//为每列分配4个大小空间 a[i] = (int*

)malloc(sizeof(int)*4); } //初始化 for (i = 0; i < 3; ++i){ for (j = 0; j < 4; ++j){ a[i][j] = i+j; } } //输出测试 for (i = 0; i < 3; ++i){ for (j = 0; j < 4; ++j){ printf ("%d ", a[i][j]); } printf ("\n"); } //释放动态开辟的空间 for (i = 0; i < 3; ++i){ free(a[i]); } free(a); return 0;}/*输出:0 1 2 31 2 3 42 3 4 5*/

uvm中如何对动态数组进行约束?

对数组大小进行限制 #define MAXN 100000 int a[MAXN] 所有的操作都在MAXN的范围下

C怎么动态定位数组中的元素?

顺序査找也叫线性查找,是一种简单的査找算法,其实现方法是从序列的起始元素开始,逐个将序列中的元素与所要查找的元素进行比较,如果序列中有元素与所要查找的元素相等,那么査找成功,如果査找到序列的最后一个元素都不存在一个元素与所要査找的元素值相等,那么表明査找失败。

vba如何依次给动态数组赋值?

在VBA中,可以使用For循环或者ForEach循环来依次给动态数组赋值。下面是两种常用的方法:

使用For循环:

Dim arr() As Variant

Dim i As Integer

ReDim arr(10) ' 定义一个长度为10的动态数组

For i = LBound(arr) To UBound(arr)

arr(i) = i ' 给数组元素赋值

Next i

使用ForEach循环:

Dim arr() As Variant

Dim element As Variant

ReDim arr(10) ' 定义一个长度为10的动态数组

For Each element In arr

element = i ' 给数组元素赋值

Next element

请注意,在使用动态数组之前,需要先使用ReDim语句定义数组的大小。以上示例中,动态数组arr的长度为10。通过循环,可以逐个给数组元素赋值。

在android开发中?

效果:layout界面布局:[html] view plaincopyprint?<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:ad="http://schemas.android.com/apk/res/android"ad:layout_width="match_parent"ad:layout_height="match_parent"ad:orientation="vertical" ><LinearLayoutad:layout_width="match_parent"ad:layout_height="30dp"ad:background="@drawable/titlebar_bg"ad:orientation="horizontal" ><ImageViewad:layout_width="wrap_content"ad:layout_height="wrap_content"ad:src="@drawable/back_44_44" /><LinearLayoutad:layout_width="match_parent"ad:layout_height="30dp"ad:gravity="center" ><TextViewad:layout_width="wrap_content"ad:layout_height="wrap_content"ad:text="课程列表"ad:textSize="20sp" /></LinearLayout></LinearLayout><ScrollViewad:id="@+id/ScrollView"ad:layout_width="fill_parent"ad:layout_height="wrap_content"ad:scrollbars="vertical" ><LinearLayoutad:id="@+id/mainLayout"ad:layout_width="match_parent"ad:layout_height="wrap_content"ad:orientation="vertical" ></LinearLayout></ScrollView></LinearLayout>httputil辅助类:

[java] view plaincopyprint?package com.tudou.activity.work4;import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import android.util.Log;public class HttpUtil {/*** 获取到流,自己处理数据* @param path* @return*/public static InputStream getInputStream(String path) {HttpURLConnection conn = null;try {URL url = new URL(path);conn = (HttpURLConnection) url.openConnection();conn.setDoInput(true);// 设置是否向httpUrlConnection输出,post请求,参数要放在http正文内conn.setDoOutput(true);conn.setReadTimeout(3000);conn.setConnectTimeout(3000);conn.setUseCaches(false);conn.setRequestMethod("POST");if (conn.getResponseCode() == 200) {Log.d("mylog", "getResponseCode:" + 200);return conn.getInputStream();}} catch (IOException e) {e.printStackTrace();} finally {if (conn != null) {conn.disconnect();}}return null;}/*** 直接返回响应体正文* @param path* @return*/public static String getResponseBody(String path,String params) {HttpURLConnection conn = null;StringBuffer result=new StringBuffer()

;try {URL url = new URL(path);conn = (HttpURLConnection) url.openConnection();conn.setDoInput(true);// 设置是否向httpUrlConnection输出,post请求,参数要放在http正文内conn.setDoOutput(true);conn.setReadTimeout(3000);conn.setConnectTimeout(3000);conn.setUseCaches(false);conn.setRequestMethod("POST");//数据输出流,该语句隐含的执行connect动作if(params!=null){DataOutputStream out = new DataOutputStream( conn.getOutputStream());//将参数写入流,刷新提交关闭流out.writeBytes(params);out.flush();out.close();}//读取连接返回的数据BufferedReader reader = new BufferedReader(new InputStreamReader( conn.getInputStream()));String inputLine = null;while (((inputLine = reader.readLine()) != null)) {result.append(inputLine);//"\n";}//关闭reader.close();if (conn.getResponseCode() == 200) {Log.d("mylog", "getResponseCode:" + 200);}} catch (IOException e) {e.printStackTrace();} finally {if (conn != null) {conn.disconnect();}}return result.toString();}}主activity:[java] view plaincopyprint?package com.tudou.activity.work4;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import com.tudou.activity.R;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.util.Log;import android.util.TypedValue;import android.view.Gravity;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.LinearLayout.LayoutParams;import android.widget.ProgressBar;import android.widget.TextView;public class HomeWork4 extends Activity {String path = "此处省略,你要请求的地址";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.homework4);LinearLayout mainLayout = (LinearLayout)findViewById(R.id.mainLayout);String result=HttpUtil.getResponseBody(path,null);// Log.d("mylog", "result:" + result);try{JSONObject obj = new JSONObject(result);JSONArray array=obj.getJSONArray("onlineCourses");for (int i = 0; i < array.length(); i++) {JSONObject course= array.getJSONObject(i);// Log.d("mylog", "course:" + course.toString());//添加左边的layoutLinearLayout leftlayout=new LinearLayout(this);//注意包android.widget.LinearLayout.LayoutParams,其它包下面的LayoutParams不起作用LayoutParams params=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);params.topMargin=10;leftlayout.setLayoutParams(params);leftlayout.setOrientation(LinearLayout.HORIZONTAL);leftlayout.setGravity(Gravity.CENTER_VERTICAL);//添加左边layout的图片ImageView imageView=new ImageView(this);params=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);imageView.setLayoutParams(params);imageView.setImageResource(R.drawable.image_default_195_130);leftlayout.addView(imageView);//添加右边的layout,分为上下2部分,上面是标题,下面是进度条LinearLayout rightlayout=new LinearLayout(this);LayoutParams rightLayoutParams=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);rightlayout.setLayoutParams(rightLayoutParams);rightlayout.setOrientation(LinearLayout.VERTICAL);//添加课程标题TextView textView=new TextView(this);params=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);textView.setLayoutParams(params);textView.setText(course.get("courseName").toString());textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);//18SP//学分,水平布局,分为左右,左:学分,,右:分值LinearLayout studyLayout=new LinearLayout(this);params=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);studyLayout.setLayoutParams(params);studyLayout.setOrientation(LinearLayout.HORIZONTAL);//添加学分TextView studyView=new TextView(this);params=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);studyView.setLayoutParams(params);studyView.setText("学分:");studyView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);studyView.setTextColor(Color.parseColor("#b6b6b6"));//第2种方法:setTextColor(Color.rgb(255, 255, 255));//添加学分值TextView studyValueView=new TextView(this);params=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);studyValueView.setLayoutParams(params);studyValueView.setText(course.get("courseCredit").toString());studyValueView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);//进度条,水平布局,分为左中右,左:学习进度,中:进度条,右:%值LinearLayout processLayout=new LinearLayout(this);params=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);processLayout.setLayoutParams(params);processLayout.setOrientation(LinearLayout.HORIZONTAL);//添加学习进度TextView processtextView=new TextView(this);params=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);processtextView.setLayoutParams(params);processtextView.setText("学习进度:");processtextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);processtextView.setTextColor(Color.parseColor("#b6b6b6"));//添加进度条ProgressBar bar=new ProgressBar(this,null,android.R.attr.progressBarStyleHorizontal);//指定进度条样式params=new LayoutParams(150, ViewGroup.LayoutParams.WRAP_CONTENT);bar.setLayoutParams(params);bar.setMax(100);bar.setProgress(10);//添加%值TextView processvaluetextView=new TextView(this);params=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);processvaluetextView.setLayoutParams(params);processvaluetextView.setText("10%");processvaluetextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);processvaluetextView.setTextColor(Color.parseColor("#b6b6b6"));//添加标题rightlayout.addView(textView);//添加学分studyLayout.addView(studyView);studyLayout.addView(studyValueView);rightlayout.addView(studyLayout);//添加进度条processLayout.addView(processtextView);processLayout.addView(bar);processLayout.addView(processvaluetextView);rightlayout.addView(processLayout);//添加左右边leftlayout.addView(rightlayout);mainLayout.addView(leftlayout);}}catch(JSONException e){e.printStackTrace();

发表评论

快捷回复: 表情:
AddoilApplauseBadlaughBombCoffeeFabulousFacepalmFecesFrownHeyhaInsidiousKeepFightingNoProbPigHeadShockedSinistersmileSlapSocialSweatTolaughWatermelonWittyWowYeahYellowdog
评论列表 (暂无评论,66人围观)

还没有评论,来说两句吧...