java用什么接收一个数组(java 用数组的方式接收用户输入的数 并输出数组 求怎么实现)
大家好,如果您还对java用什么接收一个数组不太了解,没有关系,今天就由本站为大家分享java用什么接收一个数组的知识,包括java 用数组的方式接收用户输入的数 并输出数组 求怎么实现的问题都会给大家分析到,还望可以解决大家的问题,下面我们就开始吧!
java中怎么用字符流接受字符串数组
FileInputstream.read()读取的是单个字符,是ASCII码表所能表示的字符,要是要读取像有汉字等其他ASCII不能表示的,就要用FileReader,它读取的是java支持的字符集。
另外,像要是读取图片,pdf,psd等这些东西的话,就要用FileInputStream,即文件字节流。应为存在计算机中的就是二进制,是ASCII表示的。要是读取有汉字的.txt,.就用FileReader。其实用的较多的是BuffereReader,进行逐行读取。
java 用数组的方式接收用户输入的数 并输出数组 求怎么实现
publicclassUtil{
publicstaticvoidmain(String[] args){
java.util.Scannersc=newjava.util.Scanner(System.in);
String[] arr=newString[5];
for(inti=0; i< arr.length; i++){
arr[i]= sc.next();
}
//这里使用util.Arrays的代码输出数组
System.out.println(java.util.Arrays.toString(arr));
}
}
扩展资料:java中接受用户输入的其他方法
package控制台接受输入;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.Buffer;
import java.util.Scanner;
public class InputCode{
public static void main(String[] args) throws IOException{
/*
* Scanner类中的方法
*完美
*/
Scanner input=new Scanner(System.in);
System.out.println("please input your name");
String name=input.nextLine();
System.out.println(name);
/*
*缺点:只能接受用户输入的一个字符
*/
System.out.println("enter your name");
char name1= 0;
try{
//inputstream中的read()()方法放回输入流中下一个字符
name1=(char) System.in.read();
} catch(IOException e){
e.printStackTrace();
}
System.out.println(name1);
/*
* InputStreamReader和BufferedReader方法
*优点:可以获取字符串
*缺点:获取的是int或者string人需要强转
*/
//通常,Reader所作的每个读取请求都会导致对底层字符或字节流进行相应的读取请求。因此,建议用 BufferedReader
//包装所有其 read()操作可能开销很高的 Reader(如 FileReader和 InputStreamReader)。例如,
//BufferedReader in= new BufferedReader(new FileReader("foo.in"));
System.out.println("enter your name");
InputStreamReader input1=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(input1);
String name2=in.readLine();
System.out.println(name2);
}
}
java用set和get如何存取String数组
set和get存取String数组与普通的的类型的思路一致的。不过数据的存取也有些技巧,即用好Arrays类。
通常set和 get是属性的存取器,一般称getter/setter。 set表示设置值,get表示获取值。在Eclipse中先定义好字段后,选择Source/Generate Getters and Setters可以根据选择自动生成这些方法.
String[] stringArray={"a","b","c","d","e"};
System.out.println(Arrays.toString(stringArray));//输出一个数组
// [a, b, c, d, e];从一个数组创建数组列表
ArrayList<String> arrayList= new ArrayList<String>(Arrays.asList(stringArray));
System.out.println(arrayList);
// [a, b, c, d, e]
System.out.println(arrayList.contains("a"));//检查一个数组是否包含某个值
// true
String[] aArray= new String[arrayList.size()];
arrayList.toArray(aArray);
for(String s: aArray)
System.out.println(s);
ArrayUtils.reverse(aArray);//逆向一个数组
//[e, d, c, b, a]
byte[] bytes= ByteBuffer.allocate(4).putInt(8).array();
bytes= ArrayUtils.addAll(bytes,"string".getBytes());//连接两个数组
for(byte t: bytes)
System.out.format("0x%x", t);
java中如何返回一个数组;
可以使用静态变量进行返回、用指针传递、通过返回传入数组的指针三种方法实现;
代码分别如下:
//方法1,用静态变量进行返回
char*SubFunction(void)
{
static char szText[5]="adfa";//用静态空间
//对p进行赋值操作
return szText;
}
void Caller()//这个函数调用SubFunction
{
TRACE("%s\n", SubFunction);
}
//方法2,用指针传递
void SubFunction(char*pText1, char*pText2)
{
//对pText1, pText2运算
strcpy(pText1,"love");
strcpy(pText2,"you");
return;
}
void Caller()//这个函数调用SubFunction
{
char szText1[5], szText2[5];//当然这里也可以动态分配内存
SubFunction(szText1, szText2);// szText1, szText2就是带回的值
TRACE("%s%s\n", szText1, szText2);
}
方法3通过返回传入数组的指针
#include<stdio.h>
double*copy1(double array[],double c1[],int n);
double*copy2(double array[],double c2[],int n);
void main(void)
{
int size=4;
double source[4]={1,2.3,4.5,6.7};
double first_copy[4];
double second_copy[4];
double*fp,*sp;
fp=copy1(source,first_copy,size);
printf("The first copy:%f,%f,%f,%f\n",fp[0],fp[1],fp[2],fp[3]);
sp=copy2(source,second_copy,size);
printf("The second copy:%f,%f,%f,%f\n",sp[0],sp[1],sp[2],sp[3]);
}
double*copy1(double array[],double c1[],int n)
{
int i;
for(i=0;i<n;i++)
c1[i]=array[i];
return c1;
}
double*copy2(double array[],double c2[],int n)
{
double*p;
int i;
for(i=0;i<n;i++)
{
p=&array[i];
c2[i]=*p;
}
return c2;
}
关于本次java用什么接收一个数组和java 用数组的方式接收用户输入的数 并输出数组 求怎么实现的问题分享到这里就结束了,如果解决了您的问题,我们非常高兴。