我有一个2D Arraylist矩阵,如
ArrayList[][] table = new ArrayList[10][10];
table[0][0] = new ArrayList();
table[0][1].add(10);
table[1][0].add(20);
table[1][1].add(30);
System.out.println("Value="+table[1][0].get()); //error line
System.out.println行中发生错误。 如何打印arraylist矩阵的值? 谁能建议我一个方法??
答案 0 :(得分:1)
实际的例外是在
行table[0][0] = new ArrayList();
table[0][1].add(10);
您在0,0位置添加元素并尝试将arraylist元素添加到0,1。
因此无效。
尝试
public static void main(String[] args) {
ArrayList[][] table = new ArrayList[10][10];
table[0][0] = new ArrayList();
table[0][0].add(10);
table[0][0].add(20);
table[0][0].add(30);
System.out.println("Value="+table[0][0].get(1));
}
答案 1 :(得分:1)
要使用((ActivityResHan)getActivity().getSupportFragmentManager().
findFragmentByTag("viewpageframent")).initiate(getTag());
,必须先在角括号中声明import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.design.widget.TabLayout.OnTabSelectedListener;
import android.support.design.widget.TabLayout.Tab;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
/*
* FOr simplicity i added compacted classes, but the idea is, it is suppose
* to work even if you have separate clases. you may even reslove to getting
* rid of the interface, and using request code, as the tag. so you can
* identify them there.
*/
//i am in my separate class
interface ActivityResHan{
void initiate(String fragmentTag);
}
//i am in my separate class
public class MainActivity extends AppCompatActivity {
String ANDROID_PAGER_TAG_REFERER = "android:switcher:2131296359:";
ViewPager mViewPager;
TabLayout tabl;
FrameLayout fl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fl = new FrameLayout(this);
fl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
fl.setId(2);
setContentView(fl);
getSupportFragmentManager().beginTransaction().
add(2,new ViewPagerFragment(), "viewpageframent").commit();
}
//i am in my separate class
private class ViewPagerFragment extends Fragment implements ActivityResHan{
static final int PICK_CONTACT_REQUEST = 1;
String calleer = "";
@Override
@Nullable
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.activity_main, null);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
tabl = (TabLayout) view.findViewById(R.id.tablayou);
mViewPager = (ViewPager) view.findViewById(R.id.pager);
tabl.setTabsFromPagerAdapter(new FragmentPAdapt());
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabl));
tabl.setOnTabSelectedListener(new OnTabSelectedListener() {
@Override
public void onTabUnselected(Tab arg0) {
}
@Override
public void onTabSelected(Tab arg0) {
mViewPager.setCurrentItem(arg0.getPosition());
}
@Override
public void onTabReselected(Tab arg0) {
}
});
}
@Override
public void initiate(String fragmentTag) {
calleer = fragmentTag;
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE);
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent data) {
if(!calleer.isEmpty()){
Fragment fg =getSupportFragmentManager().findFragmentByTag(calleer);
fg.onActivityResult(requestCode, resultCode, data);
calleer = "";
}else{
//run my own shit
super.onActivityResult(requestCode, resultCode, data);
}
}
}
//i am in my separate class
private class FragmentPAdapt extends FragmentPagerAdapter{
private class Thefrag extends Fragment{
TextView t;
public Thefrag() {
super();
}
@Override
@Nullable
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
LinearLayout v = new LinearLayout(MainActivity.this);
v.setBackgroundColor(Color.WHITE);
t = new TextView(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
lp.gravity = Gravity.CENTER_VERTICAL|Gravity.FILL_VERTICAL;
t.setLayoutParams(lp);
t.setText(" This is frag " +getTag().subSequence(
getTag().length()-1,
getTag().length()));
t.setTextSize(15f);
v.setClickable(true);
v.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
((ActivityResHan)getActivity().getSupportFragmentManager().
findFragmentByTag("viewpageframent")).initiate(getTag());
}
});
v.addView(t);
return v;
}
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent data) {
// TODO Auto-generated method stub
t.setText(" Contact uri is = " + data.getDataString());
super.onActivityResult(requestCode, resultCode, data);
}
}
public FragmentPAdapt() {
super(getSupportFragmentManager());
mViewPager.setAdapter(this);
}
@Override
public Fragment getItem(int arg0) {
Fragment f = getSupportFragmentManager().findFragmentByTag(
ANDROID_PAGER_TAG_REFERER + String.valueOf(arg0));
if(f == null){
return new Thefrag();
}
return f;
}
@Override
public int getCount() {
return 10;
}
@Override
public CharSequence getPageTitle(int position) {
return "FRAGMENT "+String.valueOf(position);
}
}
}
的类型。尝试使用以下代码段:
ArrayList
要插入新行,您必须执行
ArrayList
并在您必须执行的特定行上追加另一个元素
ArrayList<ArrayList<Integer>> table = new ArrayList<ArrayList<Integer>>(); //2d ArrayList
ArrayList<Integer> x = new ArrayList<Integer>();
x.add(10);
x.add(20);
table.add(x);
table.add(x);
System.out.println("Value="+table); //Prints table[][]
System.out.println("Value="+table.get(0)); //Prints table[0]
System.out.println("Value="+table.get(0).get(1)); //Prints table [0][1]
答案 2 :(得分:1)
如果您正在寻找的不仅仅是玩具项目,您应该认真考虑使用一些外部矩阵库。阵列在维护方面会很痛苦。
我可以推荐EJML。使用此库,您的代码将如下所示:
BlockMatrix64F matrix = new BlockMatrix64F(10, 10);
matrix.set(0,1,10);
matrix.set(1,0,20);
matrix.set(1,1,30);
System.out.println("Value="+matrix.get(1,0));
此外,您很可能必须在矩阵内进行一些计算。图书馆将对基本图书馆提供一些支持,并为您节省一些时间。
答案 3 :(得分:1)
您似乎认为您有一个二维数字矩阵,存储为ArrayList
。那不是你所拥有的。相反,您有一个二维矩阵,其中每个元素都是ArrayList
。这意味着您的代码中确实有3个维度。我不认为这就是你想要的。有几种方法可以使用已有的结构实现两个维度(即无需访问某些外部库)。
数组是一个易于理解的结构,所以让我们从那开始。
Number[][] table = new Number[10][10];
table[0][0] = 0;
table[0][1] = 10;
table[1][0] = 20;
table[1][1] = 30;
System.out.println("Value="+table[1][0].get());
此代码声明类型为Number
的二维数组,然后将其初始化为10行和10列。然后它部分填写数字。只要您访问已经初始化的元素,您就可以了。试图访问尚未初始化的元素(如table[3][4]
)会很糟糕。
Number[][] table = { { 0, 10 }, { 20, 30 } };
System.out.println("Value=" + table[1][0]);
这和以前一样,但是一次初始化。这个特殊的数组只有2行2列。
如果您想使用ArrayList
而不是数组,那很好。您只需要意识到ArrayList
实际上将包含其他ArrayLists
,其中每个Numbers
都包含ArrayList<ArrayList<Number>> table = new ArrayList<>();
table.add(new ArrayList<>());
table.add(new ArrayList<>());
table.get(0).add(0);
table.get(0).add(10);
table.get(1).add(20);
table.get(1).add(30);
System.out.println("Value=" + table.get(1).get(0));
。像这样:
ArrayList
在此示例中,您首先声明包含ArrayLists
的{{1}},其中包含Numbers
,并初始化外部ArrayList
。然后你创建一些内部ArrayLists
,最后给它们每个Numbers
。
您可以根据需要使用数组或ArrayLists
。您只需在访问其元素之前正确初始化它们。如何初始化取决于您选择的数据结构。
import java.util.ArrayList;
public class TwoD {
public void example1() {
Number[][] table = new Number[10][10];
table[0][0] = 0;
table[0][1] = 10;
table[1][0] = 20;
table[1][1] = 30;
System.out.println("\nExample 1");
System.out.println("Value=" + table[1][0]);
}
public void example2() {
Number[][] table = { { 0, 10 }, { 20, 30 } };
System.out.println("\nExample 2");
System.out.println("Value=" + table[1][0]);
}
public void example3() {
ArrayList<ArrayList<Number>> table = new ArrayList<>();
table.add(new ArrayList<>());
table.add(new ArrayList<>());
table.get(0).add(0);
table.get(0).add(10);
table.get(1).add(20);
table.get(1).add(30);
System.out.println("\nExample 3");
System.out.println("Value=" + table.get(1).get(0));
}
public static void main(String[] args) {
TwoD me = new TwoD();
me.example1();
me.example2();
me.example3();
}
}