public static void main(String[] args)
{
char [] d = {'a','b','c','d'};
char [] e = {'d','c','b','a'};
Arrays.sort(d);
Arrays.sort(e);
System.out.println(e); //console : abcd
System.out.println(d); //console : abcd
System.out.println(d.equals(e)); //console : false
}
Run Code Online (Sandbox Code Playgroud)
为什么阵列不相等?我可能错过了一些东西,但这让我发疯了.结果应该是真的吗?是的,我已导入java.util.Arrays.
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class arraysAsList {
public static void main(String[] args) {
String [] arrayA = {"Box","Sun","Clock","Phone"};
Integer [] arrayB = {21,27,24,7};
List listStructureA = new ArrayList();
List listStructureB = new ArrayList();
listStructureA = Arrays.asList(arrayA);
listStructureB = Arrays.asList(arrayB);
System.out.println("My first list : " + listStructureA);
System.out.println("Sun = " + listStructureA.get(1));
System.out.println("My second list : " + listStructureB);
System.out.println("24 = " + listStructureB.get(2));
}
}
Run Code Online (Sandbox Code Playgroud)
我知道int是一个原始类型,而Integer是一个类.但是在这个脚本中,当我尝试使用int而不是Integer时,我得到'index out of bounds exception'错误.之前我使用int来创建数组,int数组和Integer数组之间的区别是什么?提前致谢.
我想返回数组的奇数,但Eclipse似乎不接受我的返回array[i];代码.我认为它需要返回一个完整的数组,因为我将一个数组设置为我的方法的参数.正如我之前所说的,我需要传递一个数组并获得该数组的特定元素作为回报.即使我将该数组设为静态,如何返回单个元素?
编辑:好的,这里是:
public class newClass{
public static void main(String[] args)
{
int [] newArray= new int [4];
int [] array = {4,5,6,7};
newArray[0] = array[0]+array[1]+array[2]+array[3];
newArray[1] = array[0]*array[1]*array[2]*array[3];
newArray[2] = findOut(array);
}
public static int findOut (int [] array3)
{
int e1=0;
int e2=0;
for (int i=0; i<array3.length; i++)
{
if (array3[i]%2==0)
{
e1+=array3[i];
array3[i]=e1
return array3[i];
}
else
{
e2+=array3[i];
array3[i]=e2;
return array3[i];
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我知道这里可能存在不止一些错误,但我正在研究它,我不仅会返回奇数,我还会将它们加在一起.
class Main {
public static void main(String[] args) {
int x = 3;
int [] a = new int [x];
x+=5;
System.out.println((x+=5));
System.out.println(a.length);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么它不会影响数组"a"的长度?我究竟做错了什么?
问题是我被要求使用循环将数组长度加5.我甚至无法直接增加长度.这看起来像一个简单的问题,但我仍在学习过程中.我昨天做了但我不记得怎么做了.
public static void main(String[] args) {
int [] newArray= new int [4];
int [] array = {4,5,6,7};
oddEven(array);
newArray[0] = array[0]+array[1]+array[2]+array[3];
newArray[1] = array[0]*array[1]*array[2]*array[3];
}
public static void oddEven(int [] oddEven) {
for (int i=0; i<oddEven.length; i++) {
// Cannot convert from int to boolean
if (oddEven[i] % 2)
}
}
Run Code Online (Sandbox Code Playgroud)
忽略我在这里要管理的内容.我只是好奇为什么它不接受这个for循环中的"if"语句.正如我所说,它"无法从int转换为boolean".为什么你认为这样说?
我最近下载了android studio并创建了一个新项目.起初我没有遇到任何问题.我添加了一些操作栏按钮,然后回到我的包中看到这个错误.如果有任何错误,我会仔细检查我的xml文件.它还说"无法解决方法'添加'和'提交'".但是所有这些方法都将android.R的东西作为参数.我正在使用Android 2.1设备的操作栏支持.似乎我永远不会摆脱这些android.R问题,甚至在android studio中也没有.那可能是什么问题呢?提前致谢.
编辑:为什么要downvote?我发现很多与这个问题相关的谷歌搜索结果,但他们都是关于eclipse或者android.R和android.R解决方案,例如导入你的应用程序的R类没有帮助,因为我说这是一个支持v7动作的新主要活动优化进口的酒吧.无论如何,这是代码:
package com.example.training;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(android.R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(android.R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(android.R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) …Run Code Online (Sandbox Code Playgroud)