列表视图到Recycleview(Listview to Recycleview)
我正在尝试将我的Android Studio项目上的ListView更改为RecycleView,因为我从具有大量字符串的数据库导入数据。 我有一个适用于我的ListView的适配器,但我从不使用RecycleView,所以我不知道如何使用它。 我搜索了很多主题,但我真的无法理解如何使用它。 我希望有人可以帮助我; 这是我的代码:
fragment_one.xml (我的主要,我已经添加了RecycleView而不是ListView,同样是@id)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="4"> <Button android:id="@+id/frammentoUno" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Gestione Generale" android:onClick="FragmentOneClick" android:layout_weight="1"/> <Button android:id="@+id/frammentoDue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ultimi Allarmi" android:onClick="FragmentTwoClick" android:layout_weight="3"/> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Gestione Generale" android:id="@+id/textView3" android:layout_gravity="center_horizontal" /> <!-- A RecyclerView with some commonly used attributes --> <android.support.v7.widget.RecyclerView android:id="@+id/GetAllAllarmiListView" android:scrollbars="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:dividerHeight="1dp"/>
FrammentoUno.java
public class FrammentoUno extends AppCompatActivity { private ListView GetAllAllarmiListView; /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ private GoogleApiClient mClient; private Uri mUrl; private String mTitle; private String mDescription; private Button frammentoUno, frammentoDue; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_one); addListenerOnButton(); this.GetAllAllarmiListView = (ListView) this.findViewById(R.id.GetAllAllarmiListView); new GetAllAllarmiTask().execute(new ApiConnector()); mClient = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); mUrl = Uri.parse("android-app://com.example.andrea/http/host_path"); mTitle = "Standard Poodle"; mDescription = "The Standard Poodle stands at least 18 inches at the withers"; LayoutInflater factory = LayoutInflater.from(this); final View textEntryView = factory.inflate(R.layout.get_all_allarmi_list_view_cell, null); final CheckBox visto = (CheckBox) textEntryView.findViewById(R.id.visto); visto.setOnClickListener(btnListener); } private View.OnClickListener btnListener = new View.OnClickListener() { @Override public void onClick(View visto) { if (visto.isClickable()) { startActivity(new Intent(FrammentoUno.this, SQL_visto.class)); Toast.makeText(FrammentoUno.this, "Visto", Toast.LENGTH_LONG).show(); } else { Toast.makeText(FrammentoUno.this, "Non Visto", Toast.LENGTH_LONG).show(); } } }; public void addListenerOnButton() { frammentoUno = (Button) findViewById(R.id.frammentoUno); frammentoDue = (Button) findViewById(R.id.frammentoDue); frammentoUno.setOnClickListener(new View.OnClickListener() { //Run when button is clicked @Override public void onClick(View v) { Intent i = new Intent(FrammentoUno.this, FrammentoUno.class); startActivity(i); Toast.makeText(FrammentoUno.this, "Allarmi Generali", Toast.LENGTH_LONG).show(); } }); frammentoDue.setOnClickListener(new View.OnClickListener() { //Run when button is clicked @Override public void onClick(View v) { Intent i = new Intent(FrammentoUno.this, FrammentoDue.class); startActivity(i); Toast.makeText(FrammentoUno.this, "Controllo Ultimi Allarmi", Toast.LENGTH_LONG).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); switch (id) { case R.id.MENU_1: /* Codice di gestione della voce MENU_1 */ startActivity(new Intent(this, ControlloSbarre.class)); return true; case R.id.MENU_2: /* Codice di gestione della voce MENU_2 */ startActivity(new Intent(this, LoginActivity.class)); return true; } return false; } public void setListAdapter(JSONArray jsonArray) { this.GetAllAllarmiListView.setAdapter(new GetAllAllarmiListViewAdapter(jsonArray, this)); } private class GetAllAllarmiTask extends AsyncTask<ApiConnector, Long, JSONArray> { @Override protected JSONArray doInBackground(ApiConnector... params) { return params[0].GetAllAllarmi(); } @Override protected void onPostExecute(JSONArray jsonArray) { setListAdapter(jsonArray); } }}
GetAllAllarmiListViewAdapter.java
public class GetAllAllarmiListViewAdapter extends BaseAdapter { private JSONArray dataArray; private Activity activity; private static LayoutInflater inflater = null; public GetAllAllarmiListViewAdapter(JSONArray jsonArray, Activity a) { this.dataArray = jsonArray; this.activity = a; inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return this.dataArray.length(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // set up convert view if it is null ListCell cell; if (convertView == null) { convertView = inflater.inflate(R.layout.get_all_allarmi_list_view_cell, null); cell = new ListCell(); cell.nomeParcheggio = (TextView) convertView.findViewById(R.id.id_park); cell.data = (TextView) convertView.findViewById((R.id.data)); cell.stato = (TextView) convertView.findViewById((R.id.stato)); cell.descrizione = (TextView) convertView.findViewById((R.id.id_descrizione)); cell.targa = (TextView) convertView.findViewById((R.id.targa)); cell.azione = (TextView) convertView.findViewById((R.id.azione)); cell.dispositivo = (TextView) convertView.findViewById((R.id.dispositivo)); convertView.setTag(cell); } else { cell = (ListCell) convertView.getTag(); } //change the data of cell try { JSONObject jsonObject = this.dataArray.getJSONObject(position); cell.nomeParcheggio.setText(jsonObject.getString("nome")); cell.data.setText (jsonObject.getString("data_al")); cell.stato.setText(jsonObject.getString("stato")); cell.descrizione.setText(jsonObject.getString("descrizione")); cell.targa.setText(jsonObject.getString("targa")); cell.azione.setText(jsonObject.getString("azione")); if (jsonObject.getString("azione")=="1") { cell.dispositivo.setText(jsonObject.getString("varco")); } else if (jsonObject.getString("azione")== "2"){ cell.dispositivo.setText(jsonObject.getString("cassa")); } } catch (JSONException e) { e.printStackTrace(); } return convertView; } private class ListCell { private TextView nomeParcheggio; private TextView data; private TextView stato; private TextView descrizione; private TextView targa; private TextView azione; private TextView dispositivo; }}
get_all_allarmi_list_view_cell.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_weight="3"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Nome Park" android:id="@+id/id_park" android:layout_gravity="center_horizontal" android:padding="5dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Data" android:id="@+id/data" android:layout_gravity="center_horizontal" android:padding="5dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stato" android:id="@+id/stato" android:layout_gravity="center_horizontal" android:padding="5dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Descrizione" android:id="@+id/id_descrizione" android:layout_gravity="center_horizontal" android:padding="5dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Targa" android:id="@+id/targa" android:layout_gravity="center_horizontal" android:padding="5dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Azione" android:id="@+id/azione" android:layout_gravity="center_horizontal" android:padding="5dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Dispositivo" android:id="@+id/dispositivo" android:layout_gravity="center_horizontal" android:padding="5dp"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:id="@+id/visto" android:onClick="clickHandler"/>
AndroidManifest.xml中
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.andrea.gestionesbarre"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_citiware" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".Frammenti_LogIn_Menu.LoginActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.andrea.FrammentoUno"> </activity> <activity android:name="com.example.andrea.FrammentoDue"></activity> <!-- ATTENTION: This was auto-generated to add Google Play services to your project for App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. --> <activity android:name=".Frammenti_LogIn_Menu.ControlloSbarre"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.andrea.FrammentoUno" /> </activity> </application>
我已经在我的gradle中添加了依赖项了。
感谢帮助
I'm trying to change a ListView on my Android Studio project to a RecycleView, 'cause I import data from a database with a lot of string. I have an Adapter for my ListView, but I never use RecycleView so I don't know how to work with that. I've searched in a lot of topic but I really can't understand how to use it. I hope someone could help me; this is my codes:
fragment_one.xml (my main, I've already add RecycleView instead ListView, same @id)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="4"> <Button android:id="@+id/frammentoUno" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Gestione Generale" android:onClick="FragmentOneClick" android:layout_weight="1"/> <Button android:id="@+id/frammentoDue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ultimi Allarmi" android:onClick="FragmentTwoClick" android:layout_weight="3"/> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Gestione Generale" android:id="@+id/textView3" android:layout_gravity="center_horizontal" /> <!-- A RecyclerView with some commonly used attributes --> <android.support.v7.widget.RecyclerView android:id="@+id/GetAllAllarmiListView" android:scrollbars="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:dividerHeight="1dp"/>
FrammentoUno.java
public class FrammentoUno extends AppCompatActivity { private ListView GetAllAllarmiListView; /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ private GoogleApiClient mClient; private Uri mUrl; private String mTitle; private String mDescription; private Button frammentoUno, frammentoDue; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_one); addListenerOnButton(); this.GetAllAllarmiListView = (ListView) this.findViewById(R.id.GetAllAllarmiListView); new GetAllAllarmiTask().execute(new ApiConnector()); mClient = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); mUrl = Uri.parse("android-app://com.example.andrea/http/host_path"); mTitle = "Standard Poodle"; mDescription = "The Standard Poodle stands at least 18 inches at the withers"; LayoutInflater factory = LayoutInflater.from(this); final View textEntryView = factory.inflate(R.layout.get_all_allarmi_list_view_cell, null); final CheckBox visto = (CheckBox) textEntryView.findViewById(R.id.visto); visto.setOnClickListener(btnListener); } private View.OnClickListener btnListener = new View.OnClickListener() { @Override public void onClick(View visto) { if (visto.isClickable()) { startActivity(new Intent(FrammentoUno.this, SQL_visto.class)); Toast.makeText(FrammentoUno.this, "Visto", Toast.LENGTH_LONG).show(); } else { Toast.makeText(FrammentoUno.this, "Non Visto", Toast.LENGTH_LONG).show(); } } }; public void addListenerOnButton() { frammentoUno = (Button) findViewById(R.id.frammentoUno); frammentoDue = (Button) findViewById(R.id.frammentoDue); frammentoUno.setOnClickListener(new View.OnClickListener() { //Run when button is clicked @Override public void onClick(View v) { Intent i = new Intent(FrammentoUno.this, FrammentoUno.class); startActivity(i); Toast.makeText(FrammentoUno.this, "Allarmi Generali", Toast.LENGTH_LONG).show(); } }); frammentoDue.setOnClickListener(new View.OnClickListener() { //Run when button is clicked @Override public void onClick(View v) { Intent i = new Intent(FrammentoUno.this, FrammentoDue.class); startActivity(i); Toast.makeText(FrammentoUno.this, "Controllo Ultimi Allarmi", Toast.LENGTH_LONG).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); switch (id) { case R.id.MENU_1: /* Codice di gestione della voce MENU_1 */ startActivity(new Intent(this, ControlloSbarre.class)); return true; case R.id.MENU_2: /* Codice di gestione della voce MENU_2 */ startActivity(new Intent(this, LoginActivity.class)); return true; } return false; } public void setListAdapter(JSONArray jsonArray) { this.GetAllAllarmiListView.setAdapter(new GetAllAllarmiListViewAdapter(jsonArray, this)); } private class GetAllAllarmiTask extends AsyncTask<ApiConnector, Long, JSONArray> { @Override protected JSONArray doInBackground(ApiConnector... params) { return params[0].GetAllAllarmi(); } @Override protected void onPostExecute(JSONArray jsonArray) { setListAdapter(jsonArray); } }}
GetAllAllarmiListViewAdapter.java
public class GetAllAllarmiListViewAdapter extends BaseAdapter { private JSONArray dataArray; private Activity activity; private static LayoutInflater inflater = null; public GetAllAllarmiListViewAdapter(JSONArray jsonArray, Activity a) { this.dataArray = jsonArray; this.activity = a; inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return this.dataArray.length(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // set up convert view if it is null ListCell cell; if (convertView == null) { convertView = inflater.inflate(R.layout.get_all_allarmi_list_view_cell, null); cell = new ListCell(); cell.nomeParcheggio = (TextView) convertView.findViewById(R.id.id_park); cell.data = (TextView) convertView.findViewById((R.id.data)); cell.stato = (TextView) convertView.findViewById((R.id.stato)); cell.descrizione = (TextView) convertView.findViewById((R.id.id_descrizione)); cell.targa = (TextView) convertView.findViewById((R.id.targa)); cell.azione = (TextView) convertView.findViewById((R.id.azione)); cell.dispositivo = (TextView) convertView.findViewById((R.id.dispositivo)); convertView.setTag(cell); } else { cell = (ListCell) convertView.getTag(); } //change the data of cell try { JSONObject jsonObject = this.dataArray.getJSONObject(position); cell.nomeParcheggio.setText(jsonObject.getString("nome")); cell.data.setText (jsonObject.getString("data_al")); cell.stato.setText(jsonObject.getString("stato")); cell.descrizione.setText(jsonObject.getString("descrizione")); cell.targa.setText(jsonObject.getString("targa")); cell.azione.setText(jsonObject.getString("azione")); if (jsonObject.getString("azione")=="1") { cell.dispositivo.setText(jsonObject.getString("varco")); } else if (jsonObject.getString("azione")== "2"){ cell.dispositivo.setText(jsonObject.getString("cassa")); } } catch (JSONException e) { e.printStackTrace(); } return convertView; } private class ListCell { private TextView nomeParcheggio; private TextView data; private TextView stato; private TextView descrizione; private TextView targa; private TextView azione; private TextView dispositivo; }}
get_all_allarmi_list_view_cell.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_weight="3"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Nome Park" android:id="@+id/id_park" android:layout_gravity="center_horizontal" android:padding="5dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Data" android:id="@+id/data" android:layout_gravity="center_horizontal" android:padding="5dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stato" android:id="@+id/stato" android:layout_gravity="center_horizontal" android:padding="5dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Descrizione" android:id="@+id/id_descrizione" android:layout_gravity="center_horizontal" android:padding="5dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Targa" android:id="@+id/targa" android:layout_gravity="center_horizontal" android:padding="5dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Azione" android:id="@+id/azione" android:layout_gravity="center_horizontal" android:padding="5dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Dispositivo" android:id="@+id/dispositivo" android:layout_gravity="center_horizontal" android:padding="5dp"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:id="@+id/visto" android:onClick="clickHandler"/>
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.andrea.gestionesbarre"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_citiware" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".Frammenti_LogIn_Menu.LoginActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.andrea.FrammentoUno"> </activity> <activity android:name="com.example.andrea.FrammentoDue"></activity> <!-- ATTENTION: This was auto-generated to add Google Play services to your project for App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. --> <activity android:name=".Frammenti_LogIn_Menu.ControlloSbarre"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.andrea.FrammentoUno" /> </activity> </application>
I've already add dependencies to my gradle.
Thanks for help
原文:https://stackoverflow.com/questions/35597866
最满意答案
这种混淆与C中的数组有关,在特定的上下文中,数组隐含地衰减成指针。
更容易解释,
nums + 1
有效意味着&nums[0] + 1
。nums[0]
是int
类型,它是每个元素4个字节。 因此#号&nums[0] + 1
是在#号之后的4个字节。至于#
&nums + 1
,#是类型int(*)[3]
,它是每个元素12个字节。 因此#号&nums + 1
是#号后面的12个字节。The confusion is related to how in C, arrays implicitly decay into pointers in certain contexts.
The easier one to explain,
nums + 1
effectively means&nums[0] + 1
.nums[0]
is typeint
, which is 4 bytes per element. Thus&nums[0] + 1
is 4 bytes after&nums
.As for
&nums + 1
,&nums
is of typeint(*)[3]
, which is 12 bytes per element. Thus&nums + 1
is 12 bytes after&nums
.
相关问答
更多-
删除对象的指针是有毒的:除了赋予它们新的价值以外,不要碰它。 内存跟踪系统可能会捕获回收的指针值。 但是,我不知道这样的系统是否存在。 相关报价是3.7.4.2 [basic.stc.dynamic.deallocation]第4段: 如果给标准库中的释放函数赋予的参数是一个不是空指针值的指针,则释放函数应该释放指针引用的存储器,导致无效的所有指针指向解除分配的存储器的任何部分。 使用无效指针值(包括将它传递给释放函数)的效果未定义。 当调整一个std::vector<...>大小时,它会跳过许多hoops ...
-
这种混淆与C中的数组有关,在特定的上下文中,数组隐含地衰减成指针。 更容易解释, nums + 1有效意味着&nums[0] + 1 。 nums[0]是int类型,它是每个元素4个字节。 因此#号&nums[0] + 1是在#号之后的4个字节。 至于# &nums + 1 ,#是类型int(*)[3] ,它是每个元素12个字节。 因此#号&nums + 1是#号后面的12个字节。 The confusion is related to how in C, arrays implicitly decay i ...
-
虽然您可以使用eval()来执行此操作,但它依赖于安全的变量。 这更加安全得多 : function compute($num1, $operator, $num2) { switch($operator) { case "+": return $num1 + $num2; case "-": return $num1 - $num2; case "*": return $num1 * $num2; case "/": return $ ...
-
[expr.add]标准草案: 当具有整数类型的表达式被添加到指针或从指针中减去时,结果具有指针操作数的类型。 如果表达式P指向具有n个元素的数组对象x的元素x [i],则表达式P + J和J + P(其中J具有值j)指向(可能是假设的)元素x [i + j]如果0≤i+j≤n; 否则,行为是不确定的。 同样,如果0≤i - j≤n,则表达式P - J指向(可能是假设的)元素x [i - j] 否则,行为是不确定的。 当减去同一个数组对象的元素的两个指针时,结果的类型是实现定义的有符号整型; 这个类型应该与 ...
-
参数timeStamps的类型为MyStructure* ,表示此行: MyStructure *myStructure = &(*(timeStamps + i)); 相当于: MyStructure *myStructure = timeStamps + i; 这相当于: MyStructure *myStructure = &timeStamps[i]; 请注意,在此表达式中: &(*(timeStamps + i)) , timeStamps + i是指向索引i处元素的指针(即此元素的地址), ...
-
C ++指针字符算术(C++ pointer to char arithmetic)[2022-02-25]
“如果我给指针加1,实际添加的值将是指针指向的类型的大小?” 在C ++标准中,不能保证指针是指针指向的某个内存的字节数。 如果给指针添加一个整数n ,结果是指向该数组中第n个下一个元素的指针: int iarr[10]; int* pi = iarr; // pi points to iarr[0] int* pi2 = pi+2; // pi2 points to iarr[2] 当你看时你会得到什么,例如int repr = (int)pi; 不是由C ++标准定义的。 最流行的平台/实现将会发生 ... -
如何使用指针算法在C中将值从一个数组复制到另一个数组(How to copy values from one array to another in C with pointer arithmetic)[2022-06-29]
您的问题出在这个循环中,您从*p复制到*p2 : for(p2 = array2; p2< array2+(sizeof(array2)/sizeof(int)); p2++){ *(p2) = *(p); } 你增加p2但从不增加p ,你永远不会重置p指向array的开头。 在此循环开始时, p指向array末尾的位置。 将你的循环改为: for(p2 = array2, p = array; p2 < array2 + (sizeof(array2)/sizeof(int)) - 1; p2+ ... -
C中的C指针算法(C Pointer arithmetic in Python)[2022-05-17]
Python中类似的代码片段可能是: def somename(z): i = 0 while (....): a += z[i] b += z[i+1] c += z[i+2] i += 3 在C中, z工作方式与数组索引类似,只不过它从数组起始地址开始,而不是从0开始。在Python中没有类似的概念,因此您需要明确使用列表索引。 内部的任何东西(....)也需要修改。 我将把它作为你的练习,因为它在问题中没有具体说明。 A ... -
C数组指针算术(C array pointer arithmetic)[2021-11-07]
这是一个回复,它不是用于使用指针算法的效率的演示,而是用于显示数组索引(下标)如何转换为指针算法,应用于您的代码。 请查看此SO帖子,其中包含来自C标准的有价值信息。 你的代码也有很少的逻辑错误,从错误的数组读入和写入等等......你可以自己轻松找到它们。 这是一个有效的解决方案: #includevoid insert0(int n, int *a1, int *a2) { int p; //just a normal int for counter int j = ... -
指针指针和没有数组的算术和C中列表的动态列表(pointer to pointer & Arithmetic without array & dynamic list of list in C)[2022-05-15]
*(pathList+1) = secondPath;// IS THIS LEAGLE? 是的,这会导致代码出现问题。 由于pathList+1将超过您分配的内存块。 并且您取消引用此表达式,将导致未定义的行为。 还有关于你的例子 - int **a; *a = &val; // a now points to a pointer to val 这也是不正确的 ,因为a 未初始化并且取消引用它将再次导致UB。 在这两种情况下,您首先需要为指针分配内存然后执行操作。 *(pathList+1) = se ...