我有一个列表视图,左侧是图像图标,右侧是三行文本视图.我已经创建了布局,但在创建适配器类时,我遇到了一些问题.这是代码:
import android.app.Activity;
import android.widget.ArrayAdapter;
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[][] listRow;
private final Integer[] imageId;
public CustomList(Activity context, String[][] listRow, Integer[] imageId){
super(context, R.layout.rowlayout, listRow);
}
}
Run Code Online (Sandbox Code Playgroud)
首先,我为三个textview创建了三个1D数组,但在构造函数中我无法全部传递它们.
所以我必须创建一个2D数组,其中表中的每一行都代表有关单个列表视图项的信息.
但我无法这样做.我收到一个错误:
无法
super(android.app.Activity, int, java.lang.String[][])使用2D数组解析方法
我该如何解决这个问题?
HashMap(或)HashTable 是键控数组的一个示例。这里,索引是用户定义的键而不是通常的索引号。例如,arr["first"]=99是一个哈希映射的示例,其中 b 键为第一个,值为 99。
由于使用了键,因此需要哈希函数将键转换为索引元素,然后在数组中插入/搜索数据。此过程假设不存在冲突。
现在,给定一个要在数组中搜索的键,如果存在,则必须获取数据。因此,每次搜索之前,都必须将键转换为数组的索引号。那么如何花费 O(1) 时间呢?因为,时间复杂度也取决于哈希函数。所以时间复杂度一定是O(哈希函数的时间)。
我知道关于SO的同一问题存在多个问题.但在某个地方,我无法得到逻辑.
反转链接列表的功能如下:
void reverse()
{
struct node *curr=head, *prev=NULL;
while(curr!=NULL)
{
curr->next = prev;
prev = curr;
curr = curr->next;
}
head = prev;
}
Run Code Online (Sandbox Code Playgroud)
我使用的是全局头指针,链表中节点的结构是:
struct node
{
int data;
struct node *next;
};
struct node *head = NULL;
Run Code Online (Sandbox Code Playgroud)
这里,每当curr节点指向prev节点时,当curr节点遍历列表时,prev节点将指向列表中的最后一个节点,我将其作为头指针.
但是,此逻辑不会反转列表并仅打印第一个节点.所以,我认为代码只执行一次,但我无法发现错误.
使程序完成的其他功能:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head = NULL;
void add(int n)
{
struct node *temp = (struct node*)malloc(sizeof(struct node)); …Run Code Online (Sandbox Code Playgroud) 我得到了这个例外,Attempt to invoke virtual method 'android.view.LayoutInflater android.app.Activity.getLayoutInflater()' on a null object reference但我能找到原因.
在mainactivity中我创建了一个ListView元素,结构是:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list"
/>
Run Code Online (Sandbox Code Playgroud)
我为listview创建了一个自定义Adapter类,
class ListItem
{
public String shortDescription;
public String type;
public String time;
public int imageId;
public ListItem(String sd, String type, String time, Integer imageId)
{
this.shortDescription = sd;
this.type = type;
this.time = time;
this.imageId = imageId;
}
}
public class CustomList extends ArrayAdapter<Object>{
private Activity context;
private ListItem listItem[];
public CustomList(Activity context, ListItem li[]){
super(context, R.layout.rowlayout, li);
this.listItem …Run Code Online (Sandbox Code Playgroud) 初始化HashMap后,将插入值.但在尝试打印值时,不会打印所有值.我正在努力解决这个问题.
代码如下:
import java.util.Scanner;
import java.util.HashMap;
import java.util.Iterator;
public class test
{
public static void main(String args[]) throws Exception
{
Scanner input = new Scanner(System.in);
String inputString = input.next();
int cases = input.nextInt();
long max = 0;
HashMap<Long, Long> points = new HashMap<>();
while(cases>0)
{
points.put(input.nextLong(), input.nextLong());
cases--;
}
Iterator iterator = points.keySet().iterator();
while(iterator.hasNext())
{
long x = (Long)iterator.next();
long y = points.get(x);
if(x>max)
max = x;
if(y>max)
max = y;
}
while(inputString.length()<=max)
inputString = inputString.concat(inputString);
iterator = …Run Code Online (Sandbox Code Playgroud) 在Google Maps API v2中,该方法animateCamera用于定义要查看的缩放级别.例如,
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat1, lng1), 12));
Run Code Online (Sandbox Code Playgroud)
这里,缩放级别固定为12.如何确保此处修复的缩放值允许用户在不手动缩放的情况下查看沿路径的源点和目标点.
我知道使用试错法和一些if-else条件,可以确定值.但有更复杂的方法吗?
在尝试理解为什么字符串是不可变的原因时,我写了一个简单的程序如下:
/* Program to test string immutability */
public class StringImmutability
{
public static void main(String args[])
{
String s = "hello";
s.concat(" world"); // returns a new string object. So, the reference must point to this object for further processing like this: s = s.concat(" world");
System.out.println(s);
s = s.concat(" world");
System.out.println(s);
String s1 = s, s2 = s, s3 = s;
System.out.println(s1+" "+s2+" "+s3);
s = s.concat(" test");
//The references are modified
System.out.println(s1+" "+s2+" "+s3);
}
}
Run Code Online (Sandbox Code Playgroud)
虽然输出不符合预期: …